0

I have a list of person names which can have 3 different styles:

  1. {last name}, {first name} {middle name} (Example: Bob, Dylan Tina)
  2. {last name}, {first name} {middle initial}. (Example: Bob, Dylan T.)
  3. {last name}, {first name} (Example: Bob, Dylan)

And this is the regex which I wrote:

^[a-zA-Z]+(([' ,.-][a-zA-Z ])?[a-zA-Z]*)*$

But it doesn't work.

Zeinab Abbasimazar
  • 9,835
  • 23
  • 82
  • 131
  • Does this answer your question? [How to capture multiple repeated groups?](https://stackoverflow.com/questions/37003623/how-to-capture-multiple-repeated-groups) – Ulysse BN Dec 09 '19 at 07:43

2 Answers2

0

You could write the regex like this

^(\w+),\s(\w+)\s*(\w*\.?)$

Here is the demo.

Update the regex to like this and you can get three different groups for your three cases

^(\w+,\s\w+\s\w+)$|^(\w+,\s\w+\s\w+\.)$|^(\w+,\s\w+)$

Here is the demo.

Here is the python code

import re
s2 = "Bob, Dylan"
out = re.findall(r"^(\w+),\s(\w+)\s*(\w*\.?)$",s2)
print(out)

OUTPUT

[('Bob', 'Dylan', '')]
Albin Paul
  • 3,330
  • 2
  • 14
  • 30
0

You should use this regex:

(\w+),\s*(\w+)\s*(\w{0,}\.*)

This is the result you'll get:

>>> import re
>>> s1 = "Bob, Dylan Tina"
>>> s2 = "Bob, Dylan"
>>> s3 = "Bob, Dylan T."
>>> p = re.compile(r"(\w+),\s*(\w+)\s*(\w{0,}\.*)")
>>> re.findall(p, s1)
[('Bob', 'Dylan', 'Tina')]
>>> re.findall(p, s2)
[('Bob', 'Dylan', '')]
>>> re.findall(p, s3)
[('Bob', 'Dylan', 'T.')]
Zeinab Abbasimazar
  • 9,835
  • 23
  • 82
  • 131