Sublime Forum

Regular Expression Multi-Line Mode

#1

Normally in the Perl Regular Expression engine (and I assume Boost’s version of it too since it is based on it), multi-line mode is OFF unless turned ON by an option. This is also true of the other places I have worked with Regular Expressions (.NET and Python Regex classes, and other editors).

Note: multi-line mode changes what the \s and . special characters match. In “normal” mode they do not match \n and \r characters, but in multi-line mode they do.

Based on what I have found after 22 days of learning about Sublime Text, it would appear that Multi-Line Mode is always turned on and cannot be turned off. Is my understanding of this correct?

If this is for consistency and flexibility, it is understandable, but I think it’s important for advanced users to understand (me included).

0 Likes

#2

I have never heard of a mode where \s doesn’t match newline or carriage return characters. . by default does not match newlines. If you wanted it to, you’d use the (?s) flag at the beginning of your regex pattern.
https://www.regular-expressions.info/refmodifiers.html

0 Likes

#3

My goodness, you are so right! I thought I had previously encountered a situation that required a (?m) at the beginning of of a regex to get \s to match \r and \n (using the Python re module), but I just re-tested it, and I was mistaken! (What I thought was affecting the \s was actually affecting the ^ in my regex! I’m surprised I missed that – went to re-study the exact changes the (?m) modifier makes! I think I missed that previously because when I was learning Regexes I was also working with single lines of text! Wow! What a miss! Thank you!

And thank you for the excellent modifier reference!

P.S. My re-test made me become aware of a bug I had in some important Python code!

0 Likes