命名正则表达式组“(?P<group_name>regexp)":“P"代表什么?代表? [英] Named regular expression group "(?P<group_name>regexp)": what does "P" stand for?

查看:48
本文介绍了命名正则表达式组“(?P<group_name>regexp)":“P"代表什么?代表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Python 中,(?P...) 语法 允许通过其名称来引用匹配的字符串:

<预><代码>>>>进口重新>>>match = re.search('(?P.*) (?P.*)', 'John 123456')>>>match.group('name')'约翰'

P"代表什么?我在官方文档中找不到任何提示.>

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

解决方案

既然我们都在猜测,我不妨给出我的答案:我一直认为它代表 Python.这听起来可能很愚蠢——什么,P 代表 Python?!-- 但在我的辩护中,我依稀记得这个帖子 [强调我的]:

<块引用>

主题:声明 (?P...) 正则表达式语法扩展

来自:Guido van Rossum (gui...@CNRI.Reston.Va.US)

日期:1997 年 12 月 10 日下午 3:36:19

我对 Perl 开发人员(那些开发Perl 语言).我希望这个 (perl5-porters) 是正确的列表.一世我正在抄写 Python string-sig 因为它是大多数我在这里讨论的工作.

您可能知道 Python.我是 Python 的创造者;我是计划在年底前发布下一个主要"版本 Python 1.5今年.我希望 Python 和 Perl 在未来几年可以共存;异花授粉对两种语言都有好处.(我相信拉里当他将对象添加到 Perl 5 时,对 Python 有很好的了解;奥莱利出版关于两种语言的书籍.)

您可能知道,Python 1.5 添加了一个新的正则表达式模块,更接近于 Perl 的语法.我们试图尽可能接近在 Python 的语法中尽可能使用 Perl 语法.但是,正则表达式语法有一些特定于 Python 的扩展,它们都以 (?P .目前有两个:

(?P...) 类似于常规的分组括号,但文本
匹配完成后可访问组匹配,通过符号组名foo".

(?P=foo) 匹配与命名组匹配的字符串相同的字符串富".等价于 \1、\2 等,不同的是组被引用
按名称,而不是编号.

我希望这个特定于 Python 的扩展不会与任何Perl 正则表达式语法的未来 Perl 扩展.如果你有计划使用 (?P, 请尽快告诉我们,以便我们解决冲突.否则,如果 (?P 语法可以是永久保留给 Python 特定的语法扩展.(是有某种扩展注册表?)

Larry Wall 回复:

<块引用>

[...] 目前还没有注册——你的第一个请求来自在 perl5-porters 之外,所以这是一个非常低带宽的活动.(对不起,上周更低——我在纽约上网世界.)

无论如何,就我而言,你肯定有我的P"祝福.(显然 Perl 在这一点上不需要P".:-) [...]

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

In Python, the (?P<group_name>…) syntax allows one to refer to the matched string through its name:

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

What does "P" stand for? I could not find any hint in the official documentation.

I would love to get ideas about how to help my students remember this syntax. Knowing what "P" does stand for (or might stand for) would be useful.

解决方案

Since we're all guessing, I might as well give mine: I've always thought it stood for Python. That may sound pretty stupid -- what, P for Python?! -- but in my defense, I vaguely remembered this thread [emphasis mine]:

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

to which Larry Wall replied:

[...] 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. :-) [...]

So I don't know what the original choice of P was motivated by -- pattern? placeholder? penguins? -- but you can understand why I've always associated it with Python. Which considering that (1) I don't like regular expressions and avoid them wherever possible, and (2) this thread happened fifteen years ago, is kind of odd.

这篇关于命名正则表达式组“(?P&lt;group_name&gt;regexp)":“P"代表什么?代表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆