Talespin_Kit
Talespin_Kit

Reputation: 21877

regex to not match new line

The below regex should match only the lines that does not start with # character followed by anything.

^[^#].*

But if the buffer contains a empty line before it it matches the next line even if it starts with #.

For the following input it fails

This line is matched as expected

# this line should not be matched, but it does if the above line is empty !?

Upvotes: 10

Views: 23850

Answers (1)

Ibrahim Najjar
Ibrahim Najjar

Reputation: 19423

You can fix it like this:

^[^#\r\n].*

The problem with your original expression ^[^#].* is that [^#] was matching the newline character (the empty line), thus allowing the dot . to match the entire line after the empty one, so the dot isn't actually matching the newline, the [^#] is the one doing it.

Regex101 Demo

Upvotes: 12

Related Questions