1

i would convert all the lines here @cat\n@chicken\napple\nfruit\njuice into + : @cat : all\n+ :@chicken: all\n+ :apple : all\n+ : fruit : all\njuice : 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:

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

5
  • ^(?P<name>)$ is a pattern that only matches an empty string. Commented Aug 11, 2022 at 21:16
  • thank you for the clarification so what you think is missing there? Commented Aug 11, 2022 at 21:20
  • Try regex_replace(r'.+', r'+ : \g<0> : all' ) Commented Aug 11, 2022 at 21:25
  • unfortunately it didn't work there is something missing maybe quoting Commented Aug 11, 2022 at 21:45
  • regex_replace('.+', '+ : \\g<0> : all' )? Commented Aug 11, 2022 at 21:50

1 Answer 1

3

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 [^\r\n].

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.