Skip to main content

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"?


Comments

  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

Post a Comment

Popular posts from this blog

Slow Android emulator

I have a 2.67 GHz Celeron processor, 1.21 GB of RAM on a x86 Windows XP Professional machine. My understanding is that the Android emulator should start fairly quickly on such a machine, but for me it does not. I have followed all instructions in setting up the IDE, SDKs, JDKs and such and have had some success in staring the emulator quickly but is very particulary. How can I, if possible, fix this problem?