Home > front end >  how to split the strings into first name and last name
how to split the strings into first name and last name

Time:04-17

The namelist is:

[J. A. Rubiño-Martín, R. Rebolo, M. Aguiar, R. Génova-Santos, F. Gómez-Reñasco, J. M. Herreros, R.J. Hoyland, C. López-Caraballo, A. E. Pelaez Santos, V. Sanchez de la Rosa] 

and I need to split it into

[[J. A.], [Rubiño-Martín], [R.], [Rebolo], [M.], [Aguiar], [R.], [Génova-Santos], [F.], [Gómez-Reñasco], [J. M.], [Herreros], [R.J.], [Hoyland], [C.], [López-Caraballo], [A. E.], [Pelaez Santos], [V.], [Sanchez de la Rosa] 

using python regex

CodePudding user response:

For the given input, this regex works. The first group will match any number of tokens followed by a dot, multiple times in greedy fashion. The second group matches everything after the last dot followed by one ore more spaces.

^(. \.) \s (. )$

regex visualization

But as pointed out in the comments, it could easily break if you get names that don't follow this rather strict pattern.

  • Related