仅使用逗号拆分字符串,而不是在括号之间拆分

2024-10-02 02:24:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我在正则表达式中搜索仅带逗号的拆分字符串,而不是python中括号之间的拆分字符串:

例如:

string = '(parent son, daugther  , father ), sister'

预期结果:

['(parent son, daugther  , father )', 'sister']

谢谢你的帮助


Tags: 字符串stringsisterparent逗号sonfatherdaugther
3条回答

对于您的特定示例,我将使用正则表达式拆分,使用正向查找,即在逗号处拆分字符串(如果有的话,后面加空格),前面加上右括号:

import re

string = '(parent son, daugther  , father ), sister'

output = re.split(r'(?<=\)),\s+', string)
# ['(parent son, daugther  , father )', 'sister']

使用regex代替re,可以使用(*SKIP)(*FAIL)

import regex

str = '(parent son, daugther  , father ), sister'
res = regex.split(r'\(.+?\)(*SKIP)(*FAIL)|,', str)
print(res)

输出:

['(parent son, daugther  , father )', ' sister']

工作原理:

  • \(.+?\)正在尝试将左括号和右括号与其中的某些数据匹配
  • (*SKIP)(*FAIL)如果找到paren,则放弃匹配
  • |其他
  • ,匹配一个逗号。在这一点上,我们确信这不是双方之间的事

一般来说,正则表达式不擅长匹配嵌套/递归结构。因此,虽然可能会成功,但用手进行拆分会容易得多

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())

相关问题 更多 >

    热门问题