The value of qr print results in perl

From the documentation , I see

$rex = qr/my.STRING/is; print $rex; # prints (?si-xm:my.STRING) 

But I'm not sure how to understand (?si-xm:...) . If I print on qr/quick|lazy/ , I got (?-xism:quick|lazy) . What does this mean here (?-xism:...) ?

Thanks!

+7
source share
3 answers

As explained in the perlre man page:

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

The letters before are positive modifiers; those after it are negative modifiers. So, for example, (?-xism:quick|lazy) means that spaces and comments are not allowed in parentheses, case and dot are not case-sensitive in brackets . does not match newlines, and ^ and $ do not match line-start and line-end.

+15
source

As a note, the syntax (?FLAGS:pattern) received changes from perl 5.14.0, and the strinigification regex changed with it. Quote from perlre :

Starting with Perl 5.14, “^” (carriage or envelope accent) immediately after “?” is the abbreviated equivalent of "d-imsx". Flags (other than "d") can follow a carriage to override it. But the minus sign is not legal.

( d is one of a group of new flags in 5.14 that affects how Unixode affects regular expressions; d , by default, means acting basically like older versions of Perl).

With the addition of syntax (?^FLAGS:pattern) , the regular expression syntax changes the use of this syntax and lists only flags that differ from the default values. Therefore qr/hello/ builds like (?^:hello) (previously (?-xism:hello) ) and qr/hello/i builds like (?^i:hello) (formerly (?i-xsm:hello) ).

The advantage of this change is that if perl 5.16 was to add a new q regex modifier (for "run this match on a quantum computer"), qr/hello/ will not need to change the binding to (?d-xismq:hello) - it will be able to stay (?^:hello) since it is 5.14.

+11
source

They represent /x , /i , /s , /m if the letter appears to the left of - and the absence of a modifier if the letter appears to the right of - .

The purpose of the code is used to transmit the specified flags.

 >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 were not.

 >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 

(?:...) documented in perlre .

0
source

All Articles