106

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?

2
  • 1
    Doesn't [\s\S] work? Commented Dec 14, 2016 at 19:46
  • 1
    @WiktorStribiżew no, first of all it maches a single character, and with * quantifier it stops on EOL Commented Dec 14, 2016 at 19:49

4 Answers 4

178

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

Sign up to request clarification or add additional context in comments.

6 Comments

Yeah, that works, thanks. Tried \n, forgot about \r
@AymericBouzyaybbyk Yes, I am trying to keep this answer up-to-date, you may check the answer revision history. It did not work for some time, but the developers fixed the bug they introduced before.
how do I do a multiline negated character class?
@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.
@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.
|
17

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

2 Comments

This is helpful. Simple and easy. pipe character saves the day.
Could be much more efficient, if you used + inside like aaa(?:.+|\n)+?bbb (there is a slight difference if matching in one line e.g. aaa bbb bbb where it would match up to the latter bbb)
2

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

Comments

1

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)+?-->

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.