简体   繁体   中英

Regex split on combination of capital letter and period for abbreviated first names

This is my string:

'C. AndersonN. ChubbD. ForemanT. GurleyM. IngramA. JonesJ. Richard'

I'd like to use regex to split on the capital letter + period to generate a list like this:

['C. Anderson', 'N. Chubb', 'D. Foreman', 'T. Gurley', 'M. Ingram', 'A. Jones', 'J. Richard']

Thanks in advance!

Instead of splitting, you can use re.findall for a simpler regex expression:

import re
s = 'C. AndersonN. ChubbD. ForemanT. GurleyM. IngramA. JonesJ. Richard'
new_s = re.findall('[A-Z]\.\s[A-Z][a-z]+', s)

Output:

['C. Anderson', 'N. Chubb', 'D. Foreman', 'T. Gurley', 'M. Ingram', 'A. Jones', 'J. Richard']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM