Reputation: 1617
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?
Upvotes: 104
Views: 51496
Reputation: 39
I found my self trying to remove comments in HTML.
Based on @genevieve-warren's answer I came up with this regex which removes blocks (multiline) of HTML comments except conditional comments:
<!--[^[if](.|\n)+?-->
Upvotes: 1
Reputation: 3097
To match a 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
Upvotes: 16
Reputation: 21
If you want to exclude certain characters from the "in between" text, you can do that too. This only finds blocks where the character "c" doesn't occur between "aaa" and "bbb":
aaa([^c]|\n)+?bbb
Upvotes: 2
Reputation: 626926
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:
a
and b
chars: a[^ab\r]*b
START
and the closest STOP
words:
START[\s\S\r]*?STOP
START[^\r]*?STOP
START[\w\W]*?STOP
START
and STOP
words:
START(?:(?!START)[\s\S\r])*?STOP
See a demo screenshot below:
Upvotes: 176