Why does a regex that ends with `[^.]` match unexpectedly?
![Why does a regex that ends with `[^.]` match unexpectedly?](/_next/image?url=https%3A%2F%2Fcdn.sanity.io%2Fimages%2F80wy3gkl%2Fproduction%2F54e30bbcb677f725db7afda2ea2f51db97c37e46-1201x631.png%3Fh%3D1000&w=3840&q=75)
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