Monday, February 20, 2012

Java REGEX Operator precedence


I have a few strings:




john doe happy
george smith is happy



Here's my Regular Expression:




([a-zA-Z ]+) (is happy|happy)



I need group 1 to always be a name like "john doe" or "george smith"



I need group 2 to always be either "happy" or "is happy"



Right now, "george smith is happy" always matches up to "george smith is" and "happy" ... when I really want it to be "george smith" and "is happy"



How do I make "is happy" have precedence over "happy"?

1 comment:

  1. The + is greedy, meaning that it will match as many characters as possible, so you will never match is happy with your regex as it is written. You can change this to a lazy match by changing it to +?, which matches as few characters as possible (still one or more):

    ([a-zA-Z ]+?) (is happy|happy)

    ReplyDelete