Why does a regex that ends with `[^.]` match unexpectedly?

Why does a regex that ends with `[^.]` match unexpectedly?

Consider following line (like in a table of contents):

6.1.34.2    Some text

(there is a tab after the "2").

When searching for ^\d\+[.]\d\+[.]\d\+[^.] the line is selected (and colored from "6" to "4"), which IMHO is not correct due to the last dot in the testcase.

With ^\d\+[.]\d\+[.]\d\+\s the line is not selected (as expected).

My question is, what is wrong with the first regex?

Answer

\d\+[^.]

Here, \d\+ matches the 3, and [^.] matches the 4.

You seem to expect that \d\+ matches 34 and then [^.] will not match the .. I'm not sure if that expectation is reasonable (i.e., whether \d\+ has to be greedy).

Enjoyed this article?

Check out more content on our blog or follow us on social media.

Browse more articles