Imagine I have a multiline string, which contains tokens of the format {{ string_a }}, and can be placed either on their own line with possible leading whitespace, or on the same line as some other markup.
a: {{ string_a }}
b:
{{ string_b }}
I am trying to write a regex which will match the contents of these tokens and do a replacement, but I would also like to run some conditional logic based on whether or not they exist on their own line.
My original regex is pretty basic: \{\{\s*([A-Za-z_]+)\s*\}\} and it does a fine job of matching the tokens. However, when I try and match the leading whitespace and put that into a capturing group, it only matches those with whitespace, when really I want all the tokens regardless:
(^\s*)\{\{\s*([A-Za-z_]+)\s*\}\}
I imagine that the solution is to use some sort of lookahead/lookbehind, but whichever I try, it seems to break. Not sure if this is because of the ^ or the * in that first group, but it doesn't like it either way.
So, what I'm trying to get as my capture list is the following:
- The full token
{{ string_a }} - The inner string
string_a - Whitespace or not
\s\s\s, to do boolean conditional logic on


(\s)?