Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

Regex: Embedded pattern-match modifiers: Empty (?) modifier

by AnomalousMonk (Chancellor)
on Feb 09, 2018 at 23:47 UTC ( #1208872=perlquestion: print w/replies, xml ) Need Help??
AnomalousMonk has asked for the wisdom of the Perl Monks concerning the following question:

In code in my Re^2: creating qr from existing regex reply to bart, there is the possibility of creating an empty  (?) embedded pattern-match modifier construct. However, all the docs I've seen specify:

        One or more embedded pattern-match modifiers ...
(emphasis added), not zero. Note, though, that (all) the docs go on to say:
This is particularly useful for dynamic patterns, such as those read in from a configuration file, taken from an argument, or specified in a table somewhere.
To me, dynamic generation implies the possibility of zero modifiers: one should seek to support the most general case.

The code here and below works with an empty  (?) construct for Perl standard releases 5.8 - 5.14, all I can test with ATM. (I haven't tested with other modifier groupings than i or not-i.)

c:\@Work\Perl\monks>perl -wMstrict -le "my $string = '/.*u\/ba$/'; ;; my $flag = qr{ [msix] }xms; ;; my $convertable = my ($pattern, $modifiers) = $string =~ m{ \A \s* / (.*?) / ($flag*) \z }xms; die qq{bad rx: '$string'} unless $convertable; ;; my $rx = qr{(?$modifiers)$pattern}; print $rx; print 'A: match' if 'u/ba' =~ $rx; ;; my $ry = qr{ \A foo $rx }xms; print $ry ;; print 'B: match' if 'foolubatu/ba' =~ $ry; print 'C: match' if 'Foolubatu/ba' =~ $ry; " (?-xism:(?).*u\/ba$) A: match (?msx-i: \A foo (?-xism:(?).*u\/ba$) ) B: match

My Questions:

  • Does the code behave identically with other, later Perl releases?
  • Does anyone have experience with or insight concerning the "empty" variation of this construct?
  • Is use of  (?) a good idea, or should it be considered "experimental and subject to change"?

Give a man a fish:  <%-{-{-{-<

Replies are listed 'Best First'.
Re: Regex: Embedded pattern-match modifiers: Empty (?) modifier
by Athanasius (Chancellor) on Feb 10, 2018 at 04:39 UTC

    Hello AnomalousMonk,

    The output can’t be exactly the same across versions 5.8 to 5.14, because in 5.14.0 the (?^...) construct was added to signify default modifiers (see perl5140delta#Regular-Expressions). So, from 5.14 on, the output is:

    14:13 >perl (?^:(?).*u\/ba$) A: match (?^msx: \A foo (?^:(?).*u\/ba$) ) B: match 14:13 >

    But with that caveat, the behaviour is identical across my Strawberry Perl versions 5.12.3, 5.14.4, 5.18.2, 5.20.2, 5.22.0, 5.24.1, and 5.26.0.

    My guess — and it’s no more than a guess — is that the documentation is wrong, and should read: Zero or more embedded pattern-match modifiers ...

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

      My guess ó and itís no more than a guess ó is that the documentation is wrong, and should read: Zero or more embedded pattern-match modifiers ...

      Even if it is only a guess that the documentation is wrong: if it is underpinned by valid test cases, this warrants a bug report, so this can be evaluated and eventually be fixed.

      perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'
        Reported. Watch the thread for updates, currently it isn't clear whether the code will be fixed, or the docs.

        ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1208872]
Approved by johngg
Front-paged by haukex
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2018-08-16 15:34 GMT
Find Nodes?
    Voting Booth?
    Asked to put a square peg in a round hole, I would:

    Results (168 votes). Check out past polls.