Home > front end >  Regex Expression to parse a Last and First name string where Last name may be upper and / or concate
Regex Expression to parse a Last and First name string where Last name may be upper and / or concate

Time:10-03

I need a regex to return exactly three MATCHES (not groups) for some names input strings that can be formatted in any of the following ways:

Last First Middle

LAST First Middle

Last FirstMiddle

LAST FirstMiddle

LastFirst Middle

LASTFirst Middle

LastFirstMiddle

LASTFirstMiddle

So far, I have worked out this: ([A-Z]{2,})?([A-Z][a-z]*) It correctly returns three MATCHES for all scenarios except "LASTFirstMiddle" and "LASTFirst Middle", when the last name is capitalized and concatenated with the first name.

It is important that the result produces three MATCHES, not groups.

Please could you suggest a Regex that could support this? Thank you.

CodePudding user response:

You can use

[A-Z]{2,}(?=[A-Z][a-z]|\b)|[A-Z][a-z]*

See the regex demo. Details:

  • [A-Z]{2,}(?=[A-Z][a-z]|\b) - two or more uppercase ASCII letters followed with an uppercase ASCII letter and then a lowercase ASCII letter, or a word boundary
  • | - or
  • [A-Z][a-z]* - an uppercase ASCII letter and then zero or more lowercase ASCII letters.
  • Related