为什么第二次打印结果为None:
import re
pattern=r"(.+) 1" #notice that there is whitespace here before the backslash
match=re.match(pattern,"abc abc")
if match:
print(1) #1 was printed
match=re.match(pattern,"abc abd")
print(match) # None
此外,请给我另一个关于'\2'用法的例子。太多了。
因为在第二个示例中有不同的值,abc
和abd
。
根据py mans,number
匹配相同编号的组的内容
https://docs.python.org/3.7/library/re.html
这意味着,它匹配组的内容,而不是组的正则表达式,因此第二部分应该是相同的。这是查找其他匹配项的方法。
2
的示例如下:
(.+) (.+) 1 2
将匹配
hello kitty hello kitty