Python 如何在分隔符处拆分字符串,除非该分隔符后面有特定的模式?
我有一个Python字符串Python 如何在分隔符处拆分字符串,除非该分隔符后面有特定的模式?,python,regex,split,Python,Regex,Split,我有一个Python字符串 string = aaa1bbb1ccc1ddd 我想这样把它分开 re.split('[split at all occurrences of "1", unless the 1 is followed by a c]', string) 结果是 ['aaa', 'bbb1ccc', 'ddd'] 如何做到这一点?对regex和re模块使用负前瞻: >>> string = 'aaa1bbb1ccc1ddd' >>> imp
string = aaa1bbb1ccc1ddd
我想这样把它分开
re.split('[split at all occurrences of "1", unless the 1 is followed by a c]', string)
结果是
['aaa', 'bbb1ccc', 'ddd']
如何做到这一点?对regex和
re
模块使用负前瞻:
>>> string = 'aaa1bbb1ccc1ddd'
>>> import re
>>> re.split(r"1(?!c)", string)
['aaa', 'bbb1ccc', 'ddd']
虽然不如正则表达式漂亮,但我的以下代码返回相同的结果:
string = 'aaa1bbb1ccc1ddd'
在“1”的所有实例上拆分字符串
创建一个新的空列表,以便我们可以将所需的项目附加到
输出:
['aaa','bbb1ccc','ddd']使用一个否定的前瞻断言:
1(?!c)
(1后面不跟c)re.split(“1(?!c)”,“aaa1bbb1ccc1ddd”)
这将遍历迭代器两次(一次执行string.split('1')
,一次处理p1
),并且具有两倍的内存占用。Regex是更好的方法。谢谢@MatthewCole,这很有道理。学好正则表达式真的很有用!
string = 'aaa1bbb1ccc1ddd'
p1 = string.split('1')
new_result = []
count = 0
for j in p1:
if j.startswith('c'):
# This removes the previous element from the list and stores it in a variable.
prev_element = new_result.pop(count-1)
prev_one_plus_j = prev_element + '1' + j
new_result.append(prev_one_plus_j)
else:
new_result.append(j)
count += 1
print (new_result)