python - 命名正则表达式组 "(?Pregexp)": what d

在 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/

相关文章:

linux - 如何在shell中处理10多个参数

python - APT 命令行界面,类似于是/否输入?

python - 重新加载模块给出 NameError : name 'reload' is not

linux - 命令行参数中的 "-"(破折号)有什么魔力?

python - 字符串和字节字符串有什么区别?

android - adb 设备命令不起作用

linux - 即使在安装 npm install -g gulp 后也找不到本地 gulp 安装

linux - 如何反汇编原始 16 位 x86 机器代码?

python - 为什么 python setup.py 在 Travis CI 上说无效命令 'b

python - 解析 .py 文件,读取 AST,修改它,然后写回修改后的源代码