0

I'm trying to match a few patterns and I cannot seem to make it work.

The patterns i'm trying to match are these:

David C. Drummond
Sundar Pichai
Julie Spellman Sweet

I need a regex expression that matches all 3 groups.

What i've tried so far is: r"^(\w*)\W?(\w*|\w\.)\W?(\w*)" with many variations for the space between the words (\s, \b) and with greedy and non greedy symbol(?) after the space.

3
  • 1
    Your pattern should match all of these. In fact, your pattern matches every string that has a beginning (i.e. all strings) because everything else in it is optional. Commented Feb 4, 2017 at 10:36
  • Yet it doesn't match all of those. Commented Feb 9, 2017 at 10:39
  • Post a minimal reproducible example . Commented Feb 11, 2017 at 12:47

1 Answer 1

1

For the strings you have shown, you can use ^(\w+)\s+(?:(\w\.|\w+)\s+)?(\w+)$ to match them and have first, middle and last name contained in 3 different groups with the second being optional.

See https://regex101.com/r/O8NniM/1

Sign up to request clarification or add additional context in comments.

1 Comment

It works, but as melpomene said my regex should work but it doesn't. Could you clarify why yours works?

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.