在Python中,(?P<group_name>…)语法允许通过名称引用匹配的字符串:
>>> import re
>>> match = re.search('(?P<name>.*) (?P<phone>.*)', 'John 123456')
>>> match.group('name')
'John'
P代表什么?我在官方文件中找不到任何线索。
我很想知道如何帮助我的学生记住这个语法。知道“P”代表什么(或可能代表什么)会很有用。
在Python中,(?P<group_name>…)语法允许通过名称引用匹配的字符串:
>>> import re
>>> match = re.search('(?P<name>.*) (?P<phone>.*)', 'John 123456')
>>> match.group('name')
'John'
P代表什么?我在官方文件中找不到任何线索。
我很想知道如何帮助我的学生记住这个语法。知道“P”代表什么(或可能代表什么)会很有用。
既然我们都在猜测,我不妨给出我的答案:我一直认为它代表Python。这听起来可能很愚蠢——什么,P代表Python?!——但容我辩解一下,我隐约记得这条线索(强调我的):
Subject: Claiming (?P...) regex syntax extensions From: Guido van Rossum (gui...@CNRI.Reston.Va.US) Date: Dec 10, 1997 3:36:19 pm I have an unusual request for the Perl developers (those that develop the Perl language). I hope this (perl5-porters) is the right list. I am cc'ing the Python string-sig because it is the origin of most of the work I'm discussing here. You are probably aware of Python. I am Python's creator; I am planning to release a next "major" version, Python 1.5, by the end of this year. I hope that Python and Perl can co-exist in years to come; cross-pollination can be good for both languages. (I believe Larry had a good look at Python when he added objects to Perl 5; O'Reilly publishes books about both languages.) As you may know, Python 1.5 adds a new regular expression module that more closely matches Perl's syntax. We've tried to be as close to the Perl syntax as possible within Python's syntax. However, the regex syntax has some Python-specific extensions, which all begin with (?P . Currently there are two of them: (?P<foo>...) Similar to regular grouping parentheses, but the text matched by the group is accessible after the match has been performed, via the symbolic group name "foo". (?P=foo) Matches the same string as that matched by the group named "foo". Equivalent to \1, \2, etc. except that the group is referred to by name, not number. I hope that this Python-specific extension won't conflict with any future Perl extensions to the Perl regex syntax. If you have plans to use (?P, please let us know as soon as possible so we can resolve the conflict. Otherwise, it would be nice if the (?P syntax could be permanently reserved for Python-specific syntax extensions. (Is there some kind of registry of extensions?)
拉里·沃尔回答说:
[…目前还没有注册表——你的是第一个请求 在perl5-porters之外,所以这是一个相当低带宽的活动。 (对不起,上周的工资更低——我去纽约上网了 世界。) 不管怎样,就我所知,你肯定会把“P”和“我的”放在一起 祝福。(显然Perl在这一点上不需要'P'。: -)[…]
我不知道最初选择P的动机是什么,模式?占位符?企鹅吗?但是你可以理解为什么我总是把它和Python联系在一起。考虑到(1)我不喜欢正则表达式,尽可能避免使用它们,(2)这个线程发生在15年前,有点奇怪。
Python扩展。来自Python文档:
The solution chosen by the Perl developers was to use (?...) as the extension syntax. ? immediately after a parenthesis was a syntax error because the ? would have nothing to repeat, so this didn’t introduce any compatibility problems. The characters immediately after the ? indicate what extension is being used, so (?=foo) is one thing (a positive lookahead assertion) and (?:foo) is something else (a non-capturing group containing the subexpression foo). Python supports several of Perl’s extensions and adds an extension syntax to Perl’s extension syntax.If the first character after the question mark is a P, you know that it’s an extension that’s specific to Python
https://docs.python.org/3/howto/regex.html