python - Python 中的命名反向引用 (?P=name) 问题重新

标签 python python-3.x regex python-re backreference

我正在学习' 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然后命名的内联反向引用匹配 abcd123 .

要替换匹配项,请使用 \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/

相关文章:

python - 多次安装后图形文件夹模块将不会导入 (Mac OSX)

python - SQLite3 Python : Data type TEXT if null 'N/A'

Python递归打印嵌套哈希

javascript - 两位数的正则表达式

javascript - 获取特定字符串和特殊字符之间的字符串

python - 多核批处理

python - 如何从命令行激活 Anaconda 环境?

python-3.x - 电子邮件未发送excel附件

Python 在终端中保存 PNG 与 IPython Notebook

javascript - 如何从javascript中的字符串中提取参数(指定模式字符串)