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:
- How to trim the the
name
group? Currently I'm getting spaces in the end of it. - How to allow the name to contain numbers? If I replace
[^\d]
to.*
in thename
group, it captures the whole string. - Can you think of any better version of a regex for this string?