Home > Net >  regex just adding = breaks match
regex just adding = breaks match

Time:06-09

Why with this regex

https://regex101.com/r/wYUZCO/1

(category\s (\w ))(\s*=\s*)?(?:\s (\w ))?(?:\s (\w ))?

I can match

category fruits pear orange

but not

category fruits = pear orange

CodePudding user response:

You're matching the space after = twice; first using \s* after the = in your regex, and then with \s on (?:\s (\w )).

You could resolve this easily in many different ways, but this one is probably the simplest:

(category\s (\w ))(\s*=\s*)?(?:\s*(\w ))?(?:\s (\w ))?

(note, I changed (?:\s (\w )) to (?:\s*(\w )), in order to match a space 0 times instead of 1 times.)

  • Related