Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex that matches two or three words, but does no catpure the third if it is a specific word

I need to match a specific pattern but I'm unable to do it with regular expressions. I'm looking for people's name. It follows always the same patterns. Some combinations are:

  1. Mr. Snow
  2. Mr. John Snow
  3. Mr. John Snow (Winterfall of the nord lands)

My problem comes when sometimes I have things like: Mr. Snow and Ms. Stark. It captures also the and. So I'm looking for a regular expression that does not capture the second name only if it is and. Here I'm looking for ["Mr. Snow", "Ms. Stark"].

My best try is as follows:

(M[rs].\s\w+(?:\s[\w-]+)(?:\s\([^\)]*\))?).

Note that the second name is in a non-capturing group. Because I was thinking to use a negative look-ahead, but If I do that, the first word is not captured (because the entire name does not match), and I need that to be captured.

Any Ideas?

Here is some text to fast check.

like image 395
Xbel Avatar asked Nov 21 '25 16:11

Xbel


2 Answers

Here is my two cents:

\bM[rs]\.\h(\p{Lu}\p{Ll}+(?:[\h-]\p{Lu}\p{Ll}+)*)\b

See an online demo


  • \b - A word-boundary;
  • M[rs]\.\h - Match Mr. or Ms. followed by a horizontal whitespace;
  • (\p{Lu}\p{Ll}+(?:[\h-]\p{Lu}\p{Ll}+)*) - A capture group with a nested non-capture group to match an uppercase letter followed by lowercase letters and 0+ 2nd names concatenated through whitespace or hyphen;
  • \b - A word-boundary.
like image 126
JvdV Avatar answered Nov 24 '25 08:11

JvdV


As it is a name of a person you could also check that the first letters of the words be uppercases.

M[rs].\s[A-Z]\w+(?:\s[A-Z]\w+(?:\s\([^\)]*\))?)?

See the regex demo

like image 38
Artyom Vancyan Avatar answered Nov 24 '25 06:11

Artyom Vancyan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!