对于替换,其中匹配项和两个组代表要打印的字符。我不明白为什么替换
r'\1\2'
不起作用。
# substitutionTest.py --- test a substitution using capture groups
import traceback
try:
import re
pattern2 = r'([0-9]+)\S?(\s[A-Z]+.+$)'
# Compile regex pattern into regex object
prog2 = re.compile(pattern2)
renList = ['07) ARMSTRONG Fay Patricia', '1 157 BROSNAN Louise Margaret']
for i in range(len(renList)):
matches2 = re.search(prog2, renList[i])
if matches2:
test = re.sub(prog2, r'\1\2', renList[i])
print(test)
except Exception as e:
# to get detailed traceback
print(e)
traceback.print_exc()
输出:
07 ARMSTRONG Fay Patricia
1 157 BROSNAN Louise Margaret
这个 正则表达式演示 看起来也支持我的理论,即第二个字符串应该是
'157 BROSNAN Louise Margaret'
直到它进行替换。
我不明白。
如果您想要的只是匹配项,那么只需使用匹配组即可。
re.sub
这里是错误的选择:
import re
pattern2 = r'([0-9]+)\S?(\s[A-Z]+.+$)'
# Compile regex pattern into regex object
prog2 = re.compile(pattern2)
renList = ['07) ARMSTRONG Fay Patricia', '1 157 BROSNAN Louise Margaret']
for i in range(len(renList)):
matches2 = re.search(prog2, renList[i])
print(''.join(matches2.groups()))
输出:
07 ARMSTRONG Fay Patricia
157 BROSNAN Louise Margaret