Reputation:
I have the following Regex Pattern
/string\s+(?|(x)|(ix|iv|v?i{0,3})|([a-j]))/i
When I use this with the input
String a
I don't get a match for a
If I remove the middle or
condition or move this condition to earlier in the pattern, it picks it up.
http://regex101.com/r/uV3aL0/26
Would someone know what is going on here?
Thanks for your help.
Upvotes: 1
Views: 59
Reputation: 174766
The reason for this behavior is the inbetween v?i{0,3}
regex. If you change your regex like,
/string\s+(?|(x)|(ix|iv|v?i{1,3})|([a-j]))/i
it would work. NOrmally regex engine would take the pattern from left to right. So after string\s+
, it takes (x)
, but there isn't a character x
, so this would fail. Next it goes to (ix|iv|v?i{0,3})
and takes the first ix
. But there isn't ix
or iv
so these also fail. Next it takes this v?i{0,3})
pattern. An optional v
and an i
0 upto three times. So an inbetween empty string would be matched. So finally we got a match and the regex engine won't go for the next OR condition.
Upvotes: 1
Reputation: 241968
The problem is the following part:
v?i{0,3}
Notice that if v
is missing and there's no i
, it still matches. You can fix the problem by adding $
to the end of the regex. Or, optimize the Roman numerals in a different way:
/string\s+(?|(i?[xv]|v?i{1,3})|([a-j]))/i
Upvotes: 2