简体   繁体   English

python re.split function,如何返回完整的字符集?

[英]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']这将打印['', '12', '56', '1']

How do I use the split function to have it output the whole string, delimited by any character?如何使用拆分 function 让它 output 整个字符串,由任何字符分隔? IE ['a12', 'b56', 'c1'] IE ['a12', 'b56', 'c1']

Try to use re.findall instead re.split ( regex101 ):尝试使用re.findall代替re.splitregex101 ):

s = "a12b56c1"
import re

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

Prints:印刷:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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