正则表达式名字和姓氏

I am trying to extract firstname and lastname from a string. User can write inside my input the following: FirstName, FirstName LastName (sometimes full name, sometimes only firstname).

This is my regular expression, but it doesn't cover all the situations:

preg_match('/^(?<lastName>[^\s]+)[\s](?<firstName>.+)$/', $input, $matches);

This code will work for input like: A B, A B C D E, but not for A.

10x.

Making the space and firstname portion optional (notice the [\s] and (?<firstName>.+) are wrapped in parenthesis followed by a question mark):

^(?<lastName>[^\s]+)([\s](?<firstName>.+))?$

...against "A" captures this:

MATCH 1
lastName    [0-1]   `A`

...and against "A B, A B C D E" this:

MATCH 1
lastName    [0-1]   `A`
2.  [1-14]  ` B, A B C D E`
firstName   [2-14]  `B, A B C D E`

Try it on for size: https://regex101.com/r/zO5zR6/1