python - 命名正则表达式组 "(?P<group_name>regexp)": what does "P" stand for?

标签 python regex regex-group

在 Python 中,(?P<group_name>…) syntax 允许通过其名称引用匹配的字符串:

>>> import re
>>> match = re.search('(?P<name>.*) (?P<phone>.*)', 'John 123456')
>>> match.group('name')
'John'

“P”代表什么?我在 official documentation 中找不到任何提示。

我很想知道如何帮助我的学生记住这种语法。知道“P”代表(或可能代表)什么会很有用。

最佳答案

既然我们都在猜测,我不妨给出我的:我一直认为它代表 Python。这听起来可能很愚蠢——什么,P 代表 Python?! ——但在我的辩护中,我隐约记得this thread [强调我的]:

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?)

Larry Wall 回复:

[...] There's no registry as of now--yours is the first request from outside perl5-porters, so it's a pretty low-bandwidth activity. (Sorry it was even lower last week--I was off in New York at Internet World.)

Anyway, as far as I'm concerned, you may certainly have 'P' with my blessing. (Obviously Perl doesn't need the 'P' at this point. :-) [...]

所以我不知道最初选择P的动机是什么--模式?占位符?企鹅? ——但你可以理解为什么我总是将它与 Python 联系起来。考虑到(1)我不喜欢正则表达式并尽可能避免使用它们,以及(2)这个线程发生在 15 年前,这有点奇怪。

关于python - 命名正则表达式组 "(?P<group_name>regexp)": what does "P" stand for?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10059673/

相关文章:

javascript - 无法将 key 发送到定向 Paypal 登录页面

python - 在 vim 中获取当前缓冲区的偏移量(特别是通过 python 脚本)

javascript - 如何编写一个 javascript 正则表达式来用 html 超链接替换这种格式 [*](*) 的超链接?

.net - 匹配 .NET 中未被另一个 x 转义的 x 或\

javascript - 在一组中匹配量化单词字符多次

python - PySpark 2.1 : Importing module with UDF's breaks Hive connectivity

python - 函数参数的可变性

使用正则表达式进行Python文本处理

regex -\0 在 sed 中是什么意思?

regex - 如何让正则表达式 'or groups' 像 (foo|bar) 与 find -regex 一起使用? (操作系统)