regex_replace flags

1,174 views
Skip to first unread message

Dick Visser

unread,
Jan 5, 2017, 6:13:07 AM1/5/17
to ansible...@googlegroups.com
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

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!).



--
Dick Visser
Sr. System & Network Engineer
GÉANT

Want to join us? We're hiring: https://www.geant.org/jobs

Dick Visser

unread,
Jan 5, 2017, 6:38:56 AM1/5/17
to ansible...@googlegroups.com
I just found a nice way of supplying re.sub compile flags to regex_replace.

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
Want to join us? We're hiring: http://www.geant.org/jobs
Reply all
Reply to author
Forward
0 new messages