Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Multiline Regex not catching the correct mask

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

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

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading