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:
name group? Currently I'm getting spaces in the end of it.[^\d] to .* in the name group, it captures the whole string.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.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With