How can I split a fullname into single, medium and last name?
Obviously, it is impossible to cover all posibilities. I only want one rule: If after the word there is other word from 3 letter or less, it must join it with the next word.
Also, I am supposing that the fullname has 3 words or more.
I really have no idea even how to start.
For example:
John Martin Jackson:
- name1: John
- name2: Martin
- name3: Jackson
Steven Ponce de Leon Presley
- name1: Steven
- name2: Ponce de Leon
- name3: Presley
Michael de la Rosa Martin Jackson:
- name1: Michael de la Rosa
- name2: Martin
- name3: Jackson
:S
A really fancy regex could do that. To match one name, use
Then, combine three of them with non-matching groups, but each wrapped in one, joined by whitespaces:To make it match people without a middle name, make that optional:
Update: Don’t try to match whole names in one regex. Just use the first (simple) regex with a global flag:
Explanation:
However, I think an algorithm with some string and array functions would make it clearer what happens, and allows more customisation of the matching process: