in reply to Re^3: Recognizing 3 and 4 digit number
in thread Recognizing 3 and 4 digit number
"... clarity is always a necessity ..."
You'll get no argument from me on that one.
"My motive for always using the /ms modifier cluster (in addition to /x, of course) is to foster clarity, ..."
I think we both agree about /x: no need to discuss that any further. However, I still disagree about /ms. Compare these three lines from your code (above):
\A ((?-s: .+)) .+ ((?-s: .+)) .+ ((?-s: .+)) \z \A ((?-s: .+)) \z \A ((?-s: (?s: .+))) \z
with the eqivalent lines from my code (below):
^ ( $re_nonl ) $re_all ( $re_nonl ) $re_all ( $re_nonl ) $ ^ ( $re_nonl ) $ ^ ( $re_all ) $
Your regexes all use \ms and then need (?s and (?-s in various places. My regexes don't use \ms or (?ms at all; $re_nonl and $re_all are tiny regexes, $re_nonl doesn't use \ms or (?ms at all, $re_all only uses (?s.
"The docs say this finer control is possible: ..."
I wondered if you thought I was suggesting that level of control was not possible. If so, my apologies: that wasn't my intent. Perhaps I should have compared
qr{(?mo:...)(?ds:...)} qr{(?mo:...(?ds:...)...)}
with
qr{(?mo-ds:...)(?ds-mo:...)}mods qr{(?mo-ds:...(?ds-mo:...)...)}mods
In the code below, I've used the regexes described above ($reA, $reB & $reC). Those were written on the assumption that all were needed in the same script. I've also added $reAiso, $reBiso & $reCiso to show how I might have written these in isolation: none use the 'm' modifier; two use the 's' modifier. Finally, I added $reD as an example of when I might use both the 'm' and 's' modifiers. Throughout, I've used the input and output formats that you used in your code.
#!/usr/bin/env perl -l use strict; use warnings; use Test::More tests => 7; my $expA = "A: match, \$1 'aa ' @ 0 \$2 ' ' @ 9 \$3 'c' @ 11"; my $expB = ''; my $expC = "C: match, \$1 'aa \n bb \n cc' @ 0"; my $expD = "D: match, \$1 'aa ' @ 0 \$2 ' cc' @ 9"; my $fmtA = "A: match, \$1 '%s' @ %d \$2 '%s' @ %d \$3 '%s' @ %d"; my $fmtB = "B: match, \$1 '%s' @ %d"; my $fmtC = "C: match, \$1 '%s' @ %d"; my $fmtD = "D: match, \$1 '%s' @ %d \$2 '%s' @ %d"; my $s = "aa \n bb \n cc"; my $re_all = qr{(?sx: .+ )}; my $re_nonl = qr{(?x: [^\n]+ )}; my $reA = qr{(?x: ^ ( $re_nonl ) $re_all ( $re_nonl ) $re_all ( $re_no +nl ) $ )}; my $reB = qr{(?x: ^ ( $re_nonl ) $ )}; my $reC = qr{(?x: ^ ( $re_all ) $ )}; my $reAiso = qr{(?sx: ^ ([^\n]+) .+ ([^\n]+) .+ ([^\n]+) $ )}; my $reBiso = qr{(?x: ^ ( .+ ) $ )}; my $reCiso = qr{(?sx: ^ ( .+ ) $ )}; my $reD = qr{(?msx: \A ( .+? ) $ .+ ^ ( .+ ) \z )}; my ($gotA, $gotB, $gotC, $gotAiso, $gotBiso, $gotCiso, $gotD) = ('') x + 7; $gotA = sprintf $fmtA, $1, $-[1], $2, $-[2], $3, $-[3] if $s =~ $reA; $gotB = sprintf $fmtB, $1, $-[1] if $s =~ $reB; $gotC = sprintf $fmtC, $1, $-[1] if $s =~ $reC; $gotAiso = sprintf $fmtA, $1, $-[1], $2, $-[2], $3, $-[3] if $s =~ $re +Aiso; $gotBiso = sprintf $fmtB, $1, $-[1] if $s =~ $reBiso; $gotCiso = sprintf $fmtC, $1, $-[1] if $s =~ $reCiso; $gotD = sprintf $fmtD, $1, $-[1], $2, $-[2] if $s =~ $reD; is($gotA, $expA, 'testA'); is($gotB, $expB, 'testB'); is($gotC, $expC, 'testC'); is($gotAiso, $expA, 'testAiso'); is($gotBiso, $expB, 'testBiso'); is($gotCiso, $expC, 'testCiso'); is($gotD, $expD, 'testD');
All passed:
1..7 ok 1 - testA ok 2 - testB ok 3 - testC ok 4 - testAiso ok 5 - testBiso ok 6 - testCiso ok 7 - testD
— Ken
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^5: Recognizing 3 and 4 digit number
by AnomalousMonk (Archbishop) on Jan 08, 2017 at 23:16 UTC | |
by kcott (Archbishop) on Jan 09, 2017 at 01:32 UTC | |
by AnomalousMonk (Archbishop) on Jan 09, 2017 at 19:03 UTC | |
by kcott (Archbishop) on Jan 10, 2017 at 10:40 UTC |