Convert a string using regex_replace filter python and ansible

Question:

i would convert all the lines here @catn@chickennapplenfruitnjuice into + : @cat : alln+ :@chicken: alln+ :apple : alln+ : fruit : allnjuice : all in other hand i would get this for every line + : value : all

i would use regex_replace filter to perform the task, i don’t have too much knowledge on python, i am trying to do this:

{{ '@catn@chickennapplenfruitnjuice'| regex_replace('^(?P<name>)$', '\g<name>: ALL' , multiline=True, ignorecase=True)}} but nothing happens, i am missing something here

Asked By: medisamm

||

Answers:

You can use

regex_replace(r'.+', r'+ : g<0> : all' )

to wrap each non-empty line with + : <line_here> : all text.

Note that . matches CR chars, too, and if the line endings are CRLF, you will have to replace . with [^rn].

Here, g<0> is a replacement backreference to the whole match value, no need using named capturing groups.

Answered By: Wiktor Stribiżew