你可以使用re.findall
代替re.split
.
>>> import re
>>> s = "hi, hello! $$asdf, I am foo, bar $$jkl, cool"
>>> [j for i in re.findall(r'(.*?\$\$[^,]*),\s*|(.+)', s) for j in i if j]
['hi, hello! $$asdf', 'I am foo, bar $$jkl', 'cool']
OR
使用外部regex模块支持可变长度lookbehindre
不支持可变长度后向断言。
>>> import regex
>>> s = "hi, hello! $$asdf, I am foo, bar $$jkl, cool"
>>> regex.split(r'(?<=\$\$[^,]*),\s*', s)
['hi, hello! $$asdf', 'I am foo, bar $$jkl', 'cool']