简体   繁体   中英

python re.split function, how do I return the full character set?

I'm trying to use a regex pattern split this string into chunks seperated by any character.

s = 'a12b56c1'
import re
print(re.split('[a-zA-Z]',s))

This prints ['', '12', '56', '1']

How do I use the split function to have it output the whole string, delimited by any character? IE ['a12', 'b56', 'c1']

Try to use re.findall instead re.split ( regex101 ):

s = "a12b56c1"
import re

print(re.findall(r"\D+\d+", s))

Prints:

['a12', 'b56', 'c1']

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