Reputation: 16445
I'm fairly experienced in using regex, but I've come across something recently that I can't explain.
I was trying to make a regex that would match the test
in both: 2015_test_1_2
and test_1_2
. I came up with /(?!\d)([^_]+)/
which worked when I tested each string individually; however, when I tested both at the same time, using the /gm
modifier the 2015
from the first test matches if it isn't on the first line.
On the first line:
On the second line:
The example explains it better than I can: https://regex101.com/r/zF0iR5/4
Upvotes: 1
Views: 39
Reputation: 785611
It is because [^_]
also matches a newline.
Either move last example text to first line or change your regex to:
(?!\d)([^_\n]+)
Or else:
(?![\d\n])([^_]+)
Upvotes: 1