Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multi-line regular expressions in Visual Studio Code

I cannot figure a way to make regular expression match stop not on end of line, but on end of file in VS Code? Is it a tool limitation or there is some kind of pattern that I am not aware of?

like image 456
Dima Ogurtsov Avatar asked Dec 14 '16 19:12

Dima Ogurtsov


People also ask

What is multiline in regular expression?

Multiline option, or the m inline option, enables the regular expression engine to handle an input string that consists of multiple lines. It changes the interpretation of the ^ and $ language elements so that they match the beginning and end of a line, instead of the beginning and end of the input string.


2 Answers

It seems the CR is not matched with [\s\S]. Add \r to this character class:

[\s\S\r]+ 

will match any 1+ chars.

Other alternatives that proved working are [^\r]+ and [\w\W]+.

If you want to make any character class match line breaks, be it a positive or negative character class, you need to add \r in it.

Examples:

  • Any text between the two closest a and b chars: a[^ab\r]*b
  • Any text between START and the closest STOP words:
    • START[\s\S\r]*?STOP
    • START[^\r]*?STOP
    • START[\w\W]*?STOP
  • Any text between the closest START and STOP words:
    • START(?:(?!START)[\s\S\r])*?STOP

See a demo screenshot below:

enter image description here

like image 88
Wiktor Stribiżew Avatar answered Oct 03 '22 22:10

Wiktor Stribiżew


To matcha multi-line text block starting from aaa and ending with the first bbb (lazy qualifier)

aaa(.|\n)+?bbb 

To find a multi-line text block starting from aaa and ending with the last bbb. (greedy qualifier)

aaa(.|\n)+bbb 
like image 41
Hui Zheng Avatar answered Oct 03 '22 23:10

Hui Zheng