简体   繁体   English

上下文正则表达式拆分

[英]context regex split

I want to split a string on ', ' but not on ',, ' 我想在', '而不是',, '上分割字符串

So with input 'abc,, def, ghi' I want output 因此,使用输入'abc,, def, ghi'我想要输出

[ 'abc,, def', 'ghi' ]

I tried re.split("(?:[^,]), ",'abc,, def, ghi' ) but it removed the 'f' 我尝试了re.split("(?:[^,]), ",'abc,, def, ghi' )但是它删除了'f'

is there a way to do this? 有没有办法做到这一点?

I'd recommend using regex lookarounds: 我建议使用正则表达式环顾:

>>> re.split('(?<!,),(?=\s)', text)
['abc,, def', ' ghi']

Details 细节

(?<!   # negative lookbehind
,      # comma
)
,      
(?=    # positive lookahead
\s     # whitespace
)

Semantically, this means "split on a single comma only (comma that is not preceded or succeeded by any commas)." 从语义上讲,这意味着“ 在单个逗号上分割(任何逗号之前或之后都没有逗号)”。

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

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