perl中qr的打印结果的含义

发布于 2024-12-11 02:15:58 字数 357 浏览 0 评论 0原文

文档 中,我看到

$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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

俯瞰星空 2024-12-18 02:15:58

正如 perlre 手册页中所述:

?: 之间的任何字母都充当标志修饰符 [...]

- 之前的字母是正修饰符;后面的都是否定修饰语。因此,例如,(?-xism:quick|lazy) 表示括号内不允许包含空格和注释,括号内的部分不允许 区分大小写,点 . 匹配换行符,^$ 匹配 不匹配行开始和行结束。

As explained in the perlre man-page:

Any letters between ? and : act as flags modifiers [...]

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.

撩心不撩汉 2024-12-18 02:15:58

需要注意的是,(?FLAGS:pattern) 语法在 perl 5.14.0 中发生了变化,正则表达式字符串化也随之发生了变化。引用perlre

从 Perl 5.14 开始,“^”(脱字符号或扬抑符)紧跟在“?”之后。是相当于“d-imsx”的简写。标志(“d”除外)可以跟随插入符号来覆盖它。但减号是不合法的。

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 from perlre:

Starting in Perl 5.14, a "^" (caret or circumflex accent) immediately after the "?" is a shorthand equivalent to "d-imsx". Flags (except "d") may follow the caret to override it. But a minus sign is not legal with it.

(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. So qr/hello/ stringifies as (?^:hello) (formerly (?-xism:hello)) and qr/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.

白日梦 2024-12-18 02:15:58

如果字母出现在左侧,则它们代表 /x/i/s/m -,如果字母出现在 - 右侧,则缺少修饰符。

该代码的目的是用于传输指定了哪些标志

>perl -E"$re = qr/./s;  say qq{a\nb} =~ /a${re}b/ ? 'match' : 'no match'"
match

>perl -E"$pat = '.';  say qq{a\nb} =~ /a${pat}b/ ? 'match' : 'no match'"
no match

>perl -E"$pat = '(?s-xim:.)';  say qq{a\nb} =~ /a${pat}b/ ? 'match' : 'no match'"
match

……以及哪些没有指定。

>perl -E"$re = qr/./;  say qq{a\nb} =~ /a${re}b/s ? 'match' : 'no match'"
no match

>perl -E"$pat = '.';  say qq{a\nb} =~ /a${pat}b/s ? 'match' : 'no match'"
match

>perl -E"$pat = '(?-xism:.)';  say qq{a\nb} =~ /a${pat}b/s ? 'match' : 'no match'"
no match

(?:...) 记录在 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

>perl -E"$re = qr/./s;  say qq{a\nb} =~ /a${re}b/ ? 'match' : 'no match'"
match

>perl -E"$pat = '.';  say qq{a\nb} =~ /a${pat}b/ ? 'match' : 'no match'"
no match

>perl -E"$pat = '(?s-xim:.)';  say qq{a\nb} =~ /a${pat}b/ ? 'match' : 'no match'"
match

...and which weren't.

>perl -E"$re = qr/./;  say qq{a\nb} =~ /a${re}b/s ? 'match' : 'no match'"
no match

>perl -E"$pat = '.';  say qq{a\nb} =~ /a${pat}b/s ? 'match' : 'no match'"
match

>perl -E"$pat = '(?-xism:.)';  say qq{a\nb} =~ /a${pat}b/s ? 'match' : 'no match'"
no match

(?:...) is documented in perlre.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文