Home > other >  Multiline Regex not catching the correct mask
Multiline Regex not catching the correct mask

Time:12-09

I'm trying to catch a tag with a special syntax in a file with this regex :

([a-z0-9 >}\/])(\{(var)\:([a-z0-9\_\/\-\.] )([\?0-9] )*\})([a-z0-9 {<\/])

The tag looks like :

 {var:contactText}

But as you can see in my regex, I want to catch what's before and after the {var:something}. My expression work fine except when the expression is alone in a line. I've had m flag to prevent the problem but that's still not working.

Live example: https://regex101.com/r/6T6OJm/1/

Am I missing something? It seems to be the last part with ([a-z0-9 {<\/]) which doesn't accept line break, so what's the solution?

CodePudding user response:

Like the cat, the "multiline" modifier is a false friend. The m modifier doesn't mean the pattern will run magically over multiple lines, It only changes the meaning of the ^ and $ anchors (from start/end of the string to start/end of the line).

All what you need is to figure potential white characters using the \s class (that includes also the carriage return \r and the newline \n characters).

~
    ([a-z0-9 >}/])
    \s* ( { (var) : ([a-z0-9_/.-] ) ([?0-9] )? } ) \s*
    ([a-z0-9 {</])
~xi

demo

Note that many characters in the pattern doesn't need to be escaped. Since the pattern is a bit long, I used the x modifier to not take in account spaces in the pattern: it's more readable.

Not sure that all the capture groups are useful.

  • Related