python - 如何避免在 RegEx 拆分结果中捕获组?

标签 python regex string split

我正在尝试使用 re 来匹配以“\n”开头的模式,后跟可能的“real(r8)”,后跟零个或多个空格,然后是通过“功能”一词,然后我想在匹配发生的地方拆分字符串。所以对于这个字符串,

text = '''functional \n   function disdat \nkitkat function wakawak\nreal(r8) function noooooo \ndoit'''

我愿意:

['functional ',
 ' disdat \nkitkat function wakawak',
 ' noooooo \ndoit']

但是,

regex = re.compile(r'''\n(real\(r8\))?\s*\bfunction\b''')

regex.split(text)

返回

['functional ',
 None,
 ' disdat \nkitkat function wakawak',
 'real(r8)',
 ' noooooo \ndoit']

split 也返回匹配项的组。我如何要求它不这样做?

最佳答案

你可以像这样使用非捕获组

>>> regex = re.compile(r'\n(?:real\(r8\))?\s*\bfunction\b')
>>> regex.split(text)
['functional ', ' disdat \nkitkat function wakawak', ' noooooo \ndoit']

注意 ?:(?:real\(r8\)) 中。引用 Python documentation for (?:..)

A non-capturing version of regular parentheses. Matches whatever regular expression is inside the parentheses, but the substring matched by the group cannot be retrieved after performing a match or referenced later in the pattern.

关于python - 如何避免在 RegEx 拆分结果中捕获组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29271447/

相关文章:

python - 从消息中获取图片

java - 如何整齐地对齐字符串变量的输出?

string - 排序列表并将排序后的值添加到 bash 中的新列表

java - 从字符串中删除尖括号 < 和 > : Java regex

python - Seaborn:使用箱线图导致内存不足

python - 使用 Python 将子文件夹中的图像复制到另一个

python - .corr 如何删除 NA 和空值?

javascript - 正则表达式: Extract Dates From String

java - 在Java中使用正则表达式查找和替换而不改变文件格式

regex - 在 VBA 中返回正则表达式匹配(excel)