perl中qr的打印结果的含义
从 文档 中,我看到
$rex = qr/my.STRING/is;
print $rex; # prints (?si-xm:my.STRING)
但我不确定如何理解 ( ?si-xm:...)
。如果我在 qr/quick|lazy/
上进行打印,则会得到 (?-xism:quick|lazy)
。这里的 (?-xism:...)
又是什么意思?
谢谢!
From the documentation, I see
$rex = qr/my.STRING/is;
print $rex; # prints (?si-xm:my.STRING)
But I am not sure how to understand (?si-xm:...)
. If I do a print on qr/quick|lazy/
, I got (?-xism:quick|lazy)
. What does it mean here (?-xism:...)
too?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
正如 perlre 手册页中所述:
-
之前的字母是正修饰符;后面的都是否定修饰语。因此,例如,(?-xism:quick|lazy)
表示括号内不允许包含空格和注释,括号内的部分不允许 区分大小写,点.
不 匹配换行符,^
和$
匹配 不匹配行开始和行结束。As explained in the perlre man-page:
The letters before the
-
are positive modifiers; those after it are negative modifiers. So, for example,(?-xism:quick|lazy)
means that whitespace and comments are not allowed inside the parentheses, the parenthesized part is not case-sensitive, a dot.
does not match newlines, and^
and$
do not match line-start and line-end.需要注意的是,
(?FLAGS:pattern)
语法在 perl 5.14.0 中发生了变化,正则表达式字符串化也随之发生了变化。引用perlre
:(
d
是 5.14 中的一组新标志之一,它影响 Unicode 对正则表达式的影响;d
是默认值,意味着基本上像旧的 Perl 版本一样运行)。添加
(?^FLAGS:pattern)
语法后,正则表达式字符串化更改为使用此语法,并且仅列出与默认值不同的标志。因此qr/hello/
字符串化为(?^:hello)
(以前的(?-xism:hello)
)和qr/ hello/i
字符串化为(?^i:hello)
(以前是(?i-xsm:hello)
)。此更改的优点是,如果 perl 5.16 添加新的
q
正则表达式修饰符(用于“在量子计算机上运行此匹配”),qr/hello/
获胜不必将 stringify 更改为(?d-xismq:hello)
— 它将能够保持(?^:hello)
,就像 5.14 上一样。Just as a note, the
(?FLAGS:pattern)
syntax has gotten a change with perl 5.14.0, and regex strinigification has changed along with it. To quote fromperlre
:(
d
is one of a group of new flags in 5.14 that affects how regexes are affected by Unicode;d
, the default, means to act basically like older Perl versions).With the addition of the
(?^FLAGS:pattern)
syntax, regex stringification changes to use this syntax, and only list the flags that differ from the default. Soqr/hello/
stringifies as(?^:hello)
(formerly(?-xism:hello)
) andqr/hello/i
stringifies as(?^i:hello)
(formerly(?i-xsm:hello)
).The advantage of this change is that if perl 5.16 were to add a new
q
regex modifier (for "run this match on a quantum computer"),qr/hello/
won't have to change to stringify to(?d-xismq:hello)
— it will be able to stay(?^:hello)
as it is on 5.14.如果字母出现在左侧,则它们代表
/x
、/i
、/s
、/m
-
,如果字母出现在-
右侧,则缺少修饰符。该代码的目的是用于传输指定了哪些标志
……以及哪些没有指定。
(?:...)
记录在 perlre 中。They represent
/x
,/i
,/s
,/m
if the letter appears on the left of the-
, and the lack of the modifier if the letter appears on the right of the-
.The purpose of the code is used to transmit which flags were specified
...and which weren't.
(?:...)
is documented in perlre.