Skip to content
Advertisement

groupdict in regex

I have the following string:

string = "Malcolm Reynolds"

I wrote a regex for this which will find the first-name and last-name of user:

m = re.match(r"(?P<first_name>w+) (?P<last_name>w+)", "Malcolm Reynolds")
m.groupdict()

Result:

{'first_name': 'Malcolm', 'last_name': 'Reynolds'}

Sometimes, I don’t get the last name. At that time, my regex fails. Can any one have any idea regarding this?

Advertisement

Answer

You may use

^(?:(?:M(?:(?:is|r)?s|r)|[JS]r).?s+)?(?P<first_name>w+)(?:s+(?P<last_name>w+))?$

See the regex demo

Regex details

  • ^ – start of string
  • (?:(?:M(?:(?:is|r)?s|r)|[JS]r).?s+)? – an optional non-capturing group matching an optional char sequence: Mr, Ms, Mrs, Miss, Jr or Sr with an optional . after and 1+ whitespaces
  • (?P<first_name>w+) – Group “first_name”: one or more word chars
  • (?:s+(?P<last_name>w+))? – – an optional non-capturing group matching 1+ whitespaces and then capturing into Group “last_name” 1 or more word chars
  • $ – end of string.
Advertisement