2024-10-02 02:24:48 发布
网友
我在正则表达式中搜索仅带逗号的拆分字符串,而不是python中括号之间的拆分字符串:
例如:
string = '(parent son, daugther , father ), sister'
预期结果:
['(parent son, daugther , father )', 'sister']
谢谢你的帮助
对于您的特定示例,我将使用正则表达式拆分,使用正向查找,即在逗号处拆分字符串(如果有的话,后面加空格),前面加上右括号:
import re string = '(parent son, daugther , father ), sister' output = re.split(r'(?<=\)),\s+', string) # ['(parent son, daugther , father )', 'sister']
使用regex代替re,可以使用(*SKIP)(*FAIL):
(*SKIP)(*FAIL)
import regex str = '(parent son, daugther , father ), sister' res = regex.split(r'\(.+?\)(*SKIP)(*FAIL)|,', str) print(res)
输出:
['(parent son, daugther , father )', ' sister']
\(.+?\)
|
,
一般来说,正则表达式不擅长匹配嵌套/递归结构。因此,虽然可能会成功,但用手进行拆分会容易得多
groups = [] nesting = 0 idx = 0 for group in re.finditer(r'[,\(\)]', string): assert nesting >= 0 if group[0] == '(': nesting += 1 elif group[0] == ')': nesting -= 1 elif nesting > 0: continue # ignore commas in parens else: groups.append(string[idx:group.start()].strip()) idx = group.end() # after last group groups.append(string[idx:].strip())
对于您的特定示例,我将使用正则表达式拆分,使用正向查找,即在逗号处拆分字符串(如果有的话,后面加空格),前面加上右括号:
使用regex代替re,可以使用
(*SKIP)(*FAIL)
:输出:
工作原理:
\(.+?\)
正在尝试将左括号和右括号与其中的某些数据匹配(*SKIP)(*FAIL)
如果找到paren,则放弃匹配|
其他,
匹配一个逗号。在这一点上,我们确信这不是双方之间的事李>一般来说,正则表达式不擅长匹配嵌套/递归结构。因此,虽然可能会成功,但用手进行拆分会容易得多
相关问题 更多 >
编程相关推荐