# regex\_replace flags

**URL:** <https://forum.ansible.com/t/regex-replace-flags/23386>\
**Category:** Ansible Project\
**Created:** [January 5, 2017, 11:12am UTC](https://forum.ansible.com/t/regex-replace-flags/23386 "2017-01-05T11:12:09Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [January 5, 2017, 11:12am UTC](https://forum.ansible.com/t/regex-replace-flags/23386/1 "2017-01-05T11:12:09Z")

</div>

Hi

One of my plays tries to do a regex\_replace on the returned content of  
the uri module, which is HTML with new lines in it.  
This seems to fail as from the debug output I can see that  
regex\_replace doesn't do multiline matching.

I see that there is basically only one flag for re.sub in regex\_replace:

[https://github.com/ansible/ansible/blob/devel/lib/ansible/plugins/filter/core.py#L137-L140](https://github.com/ansible/ansible/blob/devel/lib/ansible/plugins/filter/core.py#L137-L140)

For my regex to work, I would need both the re.MULTILINE and re.DOTALL  
flags to be set for re.sub.

Instead of hard coding one or two flags as arguments, wouldn't it make  
sense to have the filter accept an argument of the raw flags?

This would make things much more flexible (and powerful!).

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [January 5, 2017, 11:38am UTC](https://forum.ansible.com/t/regex-replace-flags/23386/2 "2017-01-05T11:38:02Z")

</div>

I just found a nice way of supplying re.sub compile flags to regex\_replace.

[https://docs.python.org/2/library/re.html](https://docs.python.org/2/library/re.html), at '(?iLmsux)'

So for instance, this would not work:

regex\_replace('.\*([a-z0-9]{40}).\*', '\\1')

I just found out that I would need the re.S flag (make '.' match any char).

You can supply that as part of the regex:

regex\_replace('(?s).\*([a-z0-9]{40}).\*', '\\1')

I would preferred this over hard coded flag options in Ansible.

Dick
