我想用逗号分割字符串,除非在括号中,但我的问题是当我有括号时
这是一个例子:
b='hi, this(me,(you)) , hello(a,b)'
re.split(r',(?![^\(]*[\)])', b)
['hi', ' this(me', '(you)) ', ' hello(a,b)']
我期望的是:
['嗨', '这个(我,(你))','你好(a,b)']
我见过与我想要的类似的问题,但它不像我期望的那样工作,我不知道为什么
1- Split string at commas except when in bracket environment
2- Python - Split by comma skipping the content inside parentheses
有什么帮助吗?
最佳答案
尝试使用模式(?!\S\)|\()
例如:
import re
b = ['hi, this(me,(you)) , hello(a,b)', 'hi, this(me,(you))']
for i in b:
print(re.split(r',(?!\S\)|\()', i))
输出:
['hi', ' this(me,(you)) ', ' hello(a,b)']
['hi', ' this(me,(you))']
关于python - 用逗号分割字符串,除非在括号中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59660662/