简体   繁体   English

Python分裂不是预期的结果

[英]Python split not the expected result

I'm trying to cut a string into pieces in Python with the following code: 我正在尝试使用以下代码在Python中将字符串切成段:

re.split("[AZ][az]?[0-9]*","CO2")

I expect some result like this: 我期望这样的结果:

["C","O2"]

however I get: 但是我得到:

['','','']

Since you have no capturing groups in your re, the things you split on are not part of the result. 由于您的资源中没有捕获组,因此拆分的结果不属于结果。 What you're getting is the empty string before the 'C', the empty string between 'C' and 'O2' and the empty string after 'O2'. 您得到的是'C'之前的空字符串,'C'和'O2'之间的空字符串以及'O2'之后的空字符串。

If you use 如果您使用

re.split("([A-Z][a-z]?[0-9]*)","CO2")

you'll get 你会得到

['', 'C', '', 'O2', '']

One dirty approach would be to use 一种肮脏的方法是使用

filter(None, re.split("([A-Z][a-z]?[0-9]*)","CO2"))

to remove elements that are not truthy (in this case, empty strings). 删除不真实的元素(在这种情况下为空字符串)。

If you want to split 如果要拆分

Use look arounds like this: 像这样使用环顾四周:

(?<=[A-Z\d])(?=[A-Z])

Regex live here. 正则表达式住在这里。


Else 其他

To simply match (without split): 要简单匹配(不拆分):

[A-Z][a-z]?\d*

Regex live here. 正则表达式住在这里。


Hope it helps. 希望能帮助到你。

You can also use this one: 您也可以使用以下一种:

>>> import re
>>> x = re.findall(r"^([A-Z][a-z]?)([A-Z][0-9]*)$","CO2")
>>> list(x[0])
['C', 'O2']

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

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