3

I need to separate a name from a numeric ID in a string like

Mr. John Smith 1111

where the ID in the end is optional.

So it should be 2 capturing groups - one for the name and the optional other for the numeric ID.

I've come up with the following regular expression:

/^(?P<name>[^\d]+)(?P<id>(?<= )\d+)?$/

The questions are:

  1. How to trim the the name group? Currently I'm getting spaces in the end of it.
  2. How to allow the name to contain numbers? If I replace [^\d] to .* in the name group, it captures the whole string.
  3. Can you think of any better version of a regex for this string?
super.t
  • 2,526
  • 7
  • 32
  • 51

1 Answers1

4

You may use

^(?P<name>.+?)(?:\s+(?P<id>\d+))?$

See the regex demo.

Details

  • ^ - start of string
  • (?P<name>.+?) - Group "name" that captures 1 or more chars other than line break chars as few as possible as *? is a lazy quantifier (i.e. this pattern matches the first char first, and then is skipped, the subsequent subpatterns are tried, and only when they fail, this one is "expanded")
  • (?:\s+(?P<id>\d+))? - an optional non-capturing group that matches
    • \s+ - 1+ whitespace chars
    • (?P<id>\d+) - Group "id": 1+ digits
  • $ - end of string.
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563