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:
"(?adlupimsx-imsx)"(emphasis added), not zero. Note, though, that (all) the docs go on to say:
"(?^alupimsx)"
One or more embedded pattern-match modifiers ...
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 (Archbishop) on Feb 10, 2018 at 04:39 UTC | |
by shmem (Chancellor) on Feb 10, 2018 at 14:21 UTC | |
by choroba (Cardinal) on Feb 12, 2018 at 14:09 UTC | |
by khw (Acolyte) on Mar 23, 2019 at 18:37 UTC |
Back to
Seekers of Perl Wisdom