我正在学习' re
' Python 的一部分,以及命名模式 (?P=name)
把我弄糊涂了
当我使用 re.sub()
时为了交换数字和字符,模式'(?P=name)
' 不起作用,但模式 ' \N
' 和 ' \g<name>
' 仍然有意义。代码如下:
[IN]print(re.sub(r'(?P<digit>\d{3})-(?P<char>\w{4})', r'(?P=char)-(?P=digit)', '123-abcd'))
[OUT] (?P=char)-(?P=digit)
[IN] print(re.sub(r'(?P<digit>\d{3})-(?P<char>\w{4})', r'\2-\1', '123-abcd'))
[OUT] abcd-123
[IN] print(re.sub(r'(?P<digit>\d{3})-(?P<char>\w{4})', r'\g<char>-\g<digit>', '123-abcd'))
[OUT] abcd-123
为什么我用(?P=name)
做替代失败?
以及如何正确使用?
我正在使用 Python 3.5
最佳答案
(?P=name)
是一个内联(模式内)反向引用。您可以在正则表达式模式中使用它来匹配相应命名捕获组捕获的相同内容,请参阅 Python Regular Expression Syntax reference :
(?P=name)
A backreference to a named group; it matches whatever text was matched by the earlier group named name.
参见 this demo : (?P<digit>\d{3})-(?P<char>\w{4})&(?P=char)-(?P=digit)
火柴123-abcd&abcd-123
因为“数字”组匹配并捕获了 123
, "char"组捕获 abcd
然后命名的内联反向引用匹配 abcd
和 123
.
要替换匹配项,请使用 \1
, \g<1>
或 \g<char>
re.sub
的语法更换图案。不要使用 (?P=name)
为此目的:
repl can be a string or a function... Backreferences, such as
\6
, are replaced with the substring matched by group 6 in the pattern...
In string-type repl arguments, in addition to the character escapes and backreferences described above,\g<name>
will use the substring matched by the group named name, as defined by the(?P<name>...)
syntax.\g<number>
uses the corresponding group number;\g<2>
is therefore equivalent to\2
, but isn’t ambiguous in a replacement such as\g<2>0
.\20
would be interpreted as a reference to group 20, not a reference to group 2 followed by the literal character '0'. The backreference\g<0>
substitutes in the entire substring matched by the RE.
关于python - Python 中的命名反向引用 (?P=name) 问题重新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46021148/