0

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 Answer 1

3

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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.