python - 空字符串而不是不匹配的组错误

标签 python regex python-2.x

我有这段代码:

for n in (range(1,10)):
    new = re.sub(r'(regex(group)regex)?regex', r'something'+str(n)+r'\1', old, count=1)

它抛出不匹配的组错误。但如果它不匹配,我想在那里添加空字符串而不是抛出错误。我怎样才能做到这一点?

注意:我的完整代码比这个例子复杂得多。但是,如果您找到更好的解决方案如何遍历匹配项并在其中添加数字,您可以分享。我的完整代码:

for n in (range(1,(text.count('soutez')+1))):
    text = re.sub(r'(?i)(\s*\{{2}infobox medaile reprezentant(ka)?\s*\|\s*([^\}]*)\s*\}{2}\s*)?\{{2}infobox medaile soutez\s*\|\s*([^\}]*)\s*\}{2}\s*', r"\n | reprezentace"+str(n)+r" = \3\n | soutez"+str(n)+r" = \4\n | medaile"+str(n)+r" = \n", text, count=1)

最佳答案

根本原因

在 Python 3.5 之前,Python re.sub 中对失败捕获组的反向引用不会填充空字符串。这是 Bug 1519638 description at bugs.python.org .因此,当对未参与比赛的组使用反向引用时会导致错误。

有两种方法可以解决这个问题。

解决方案 1:添加空替代项以使可选组成为必需的

您可以将所有可选的捕获组(那些结构如 (\d+)?)替换为具有空替代项(即 (\d+|))的强制性捕获组。

这里是 an example of the failure :

import re
old = 'regexregex'
new = re.sub(r'regex(group)?regex', r'something\1something', old)
print(new)

Replacing one line

new = re.sub(r'regex(group|)regex', r'something\1something', old)

有效。

解决方案 2:在替换中使用 lambda 表达式并检查组是否不是 None

如果您在另一个可选组中有可选组,则此方法是必要的。

您可以在替换部分使用 lambda 来检查组是否已初始化,而不是 None,使用 lambda m: m.group(n) 或 '' . 在您的案例中使用此解决方案,因为您在替换模式中有两个反向引用 - #3 和 #4,但是 some matches (参见匹配 1 和 3)没有初始化捕获组 3。发生这种情况是因为整个第一部分 - (\s*\{{2}funcA(ka|)\s*\|\s*([^}]*)\s*\}{2}\s *|) - 不参与比赛,内部捕获组 3(即 ([^}]*))只是 即使在添加空的选择

re.sub(r'(?i)(\s*\{{2}funcA(ka|)\s*\|\s*([^\}]*)\s*\}{2}\s*|)\{{2}funcB\s*\|\s*([^\}]*)\s*\}{2}\s*', 
r"\n | funcA"+str(n)+r" = \3\n | funcB"+str(n)+r" = \4\n | string"+str(n)+r" = \n", 
text, 
count=1)

应该重写为

re.sub(r'(?i)(\s*{{funcA(ka|)\s*\|\s*([^}]*)\s*}}\s*|){{funcB\s*\|\s*([^}]*)\s*}}\s*', 
lambda m: r"\n | funcA"+str(n)+r" = " + (m.group(3) or '') + "\n | funcB" + str(n) + r" = " + (m.group(4) or '') + "\n | string" + str(n) + r" = \n", 
text, 
count=1)  

参见 IDEONE demo

import re
 
text = r'''
 
{{funcB|param1}}
*some string*
{{funcA|param2}}
{{funcB|param3}}
*some string2*
 
{{funcB|param4}}
*some string3*
{{funcAka|param5}}
{{funcB|param6}}
*some string4*
'''
 
for n in (range(1,(text.count('funcB')+1))):
    text = re.sub(r'(?i)(\s*\{{2}funcA(ka|)\s*\|\s*([^\}]*)\s*\}{2}\s*|)\{{2}funcB\s*\|\s*([^\}]*)\s*\}{2}\s*', 
    lambda m: r"\n | funcA"+str(n)+r" = "+(m.group(3) or '')+"\n | funcB"+str(n)+r" = "+(m.group(4) or '')+"\n | string"+str(n)+r" = \n", 
    text, 
    count=1) 
    
assert text == r'''
| funcA1 =
| funcB1 = param1
| string1 =
*some string*
| funcA2 = param2
| funcB2 = param3
| string2 =
*some string2*
| funcA3 =
| funcB3 = param4
| string3 =
*some string3*
| funcA4 = param5
| funcB4 = param6
| string4 =
*some string4*
'''
print 'ok'

关于python - 空字符串而不是不匹配的组错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35516298/

相关文章:

python - 元类 __init__ 方法的 dct 参数的目的是什么?

python - 如果满足某些条件,如何继承django中的模板?

python - 损失一开始就等于0

python - PyCharm Community 2017.2 中 undefined variable 没有警告

ios - 如何创建满足包含空格和多个单词的字符串的正则表达式?

python - 筛选“pandas”中不包含字母(alpha)的所有行

python - 使用 Python 抓取网页 JavaScript 页面

python - 从包含映射到值的索引的字典中创建 Pandas 数据框

php - 具有指数运行时间的 PCRE 正则表达式

python - 为什么 input() 在我按下 enter 时会给出 SyntaxError?