Help with replaceRE() for multiple lines

If I wanted to insert an ‘i’ at the beginning of every line from a readFile(), does anyone know how to do that?

I am trying readFile "file.txt" | replaceRE "^" "i" but that is only working for the first line in the file. I can’t find a way for that regex to be made global or multiline. “/^/gm” and the link don’t work to replace anything.

Edit: As a very hacky workaround, I am using replace() to replace “\n” with “_nl_i”, then using replace again to replace “nl” with “\n”. Strange that replace() works multiline but replaceRE() doesn’t seem to.

If there is a better way to handle this, I’d love to hear it.

Hugo (Go Templates) using RE2 Regex Processor, RE2 Wiki

To set multiline flags on RegEx pattern you write it like this:

{{ readFile "file.txt" | replaceRE `(?m:^__PATTERN__)` "i" }}

I recommend to wrap your regex pattern using backtick ( ` ) instead double quotes ( " ), so you dont need to worry about character escaping inside pattern.

3 Likes

Thanks, this is exactly what I needed.

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.