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?
Asked
Active
Viewed 3.4k times
83
-
1Doesn't `[\s\S]` work? – Wiktor Stribiżew Dec 14 '16 at 19:46
-
1@WiktorStribiżew no, first of all it maches a single character, and with `*` quantifier it stops on EOL – Dima Ogurtsov Dec 14 '16 at 19:49
2 Answers
137
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
aandbchars:a[^ab\r]*b - Any text between
STARTand the closestSTOPwords:START[\s\S\r]*?STOPSTART[^\r]*?STOPSTART[\w\W]*?STOP
- Any text between the closest
STARTandSTOPwords:START(?:(?!START)[\s\S\r])*?STOP
See a demo screenshot below:
Wiktor Stribiżew
- 561,645
- 34
- 376
- 476
-
3
-
1@AymericBouzyaybbyk Yes, I am trying to keep this answer up-to-date, you may check the [answer revision history](https://stackoverflow.com/posts/41151078/revisions). It did not work for some time, but the developers fixed the bug they introduced before. – Wiktor Stribiżew Jan 02 '19 at 13:41
-
-
1@MichaelJohnston The linebreak is matched with `\r`/`\n`. Adding that to a negated character class can be done with the help of alternation, or pattern re-writing. In the generic form, it may look like `(?:\r|[^pattern])*`. I have just tested and `[^pattern\r]*` works, too, although this looks weird. – Wiktor Stribiżew Feb 26 '19 at 22:45
-
@WiktorStribiżew it does look weird, and there are some other weird things...I _think_ what's going on is if you use `\n` anywhere in the regex, `*` & `[^]` etc. start matching newlines. – Michael Johnston Mar 05 '19 at 18:04
9
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
Hui Zheng
- 1,666
- 11
- 14