Bram Vanroy
Bram Vanroy

Reputation: 28475

RegEx not behaving as expected, but works when x modifier

I have this RegEx:

(?<=\s)(?i)bananas?(\*(x|u|z))?(?=<VNW) 

Which should find hits such as:

banana*x
bananas
Bananas*z

only when preceded by a space (white space character is fine I guess? Is there a shorthand for only a space?) and followed by <VNW.

However, when testing with an online regex tester, this does not behave as expected. Try for instance bananas*z<VNW. It won't be a match. However, when adding the x modifier, it seems to work fine! Change the RegEx to:

(?<=\s)(?ix)bananas?(\*(x|u|z))?(?=<VNW)

And it ought to work fine. But I don't get why? There's not a single space in my query, nor is there in the regex. What's going on?

Upvotes: 0

Views: 32

Answers (1)

nu11p01n73R
nu11p01n73R

Reputation: 26667

The regex is fine expect that you have an extra space at the end.

Remove that and it will work fine

(?<=\s)(?i)bananas?(\*(x|u|z))?(?=<VNW)

Regex example link

Upvotes: 2

Related Questions