in reply to Re^2: Recognizing 3 and 4 digit number
in thread Recognizing 3 and 4 digit number
We definitely seem to be at odds with /m and /s. ... I rarely need those: sometimes I need one of them; I need both far less often.
My motive for always using the /ms modifier cluster (in addition to /x, of course) is to foster clarity, and clarity is always a necessity :) Clarity is improved because the . ^ $ operators have unvarying behaviors. Sometimes one is forced to be devious and must sacrifice clarity of expression, but that's what comments are for!
... the qr{(?mods:...)} form over the qr{...}mods form ... The latter form makes the modifiers global: you can't get finer control such as qr{(?mo:...)(?ds:...)} or qr{(?mo:...(?ds:...)...)}.
The docs say this finer control is possible: (?mo-ds) and (?mo-ds:pattern) are rigorously scoped:
(Tricky to put together a meaningful example for this!)c:\@Work\Perl\monks>perl -wMstrict -le "my $s = qq{aa \n bb \n cc}; ;; print qq{A: match, \$1 '$1' @ $-[1] \$2 '$2' @ $-[2] \$3 '$3' @ $-[ +3]} if $s =~ m{ \A ((?-s: .+)) .+ ((?-s: .+)) .+ ((?-s: .+)) \z }xms; ;; print qq{B: match, \$1 '$1' @ $-[1]} if $s =~ m{ \A ((?-s: .+)) \z }xms; ;; print qq{C: match, \$1 '$1' @ $-[1]} if $s =~ m{ \A ((?-s: (?s: .+))) \z }xms; " A: match, $1 'aa ' @ 0 $2 ' ' @ 9 $3 'c' @ 11 C: match, $1 'aa bb cc' @ 0
That said, I would never write regex A as above, but rather as:
Don't mess with dot (or ^ $ either): much less potential for brain-hurt.c:\@Work\Perl\monks>perl -wMstrict -le "my $s = qq{aa \n bb \n cc}; ;; print qq{A: match, \$1 '$1' @ $-[1] \$2 '$2' @ $-[2] \$3 '$3' @ $-[ +3]} if $s =~ m{ \A ([^\n]+) .+ ([^\n]+) .+ ([^\n]+) \z }xms; " A: match, $1 'aa ' @ 0 $2 ' ' @ 9 $3 'c' @ 11
Update: Another version of regex A:
In the context of global dot-matches-newline behavior, successive (?-s) and (?s) turn newline matching off and on, respectively. Again, I wouldn't actually write a regex this way unless my feet were being held to the fire.c:\@Work\Perl\monks>perl -wMstrict -le "my $s = qq{aa \n bb \n cc}; ;; print qq{A: match, \$1 '$1' @ $-[1] \$2 '$2' @ $-[2] \$3 '$3' @ $-[ +3]} if $s =~ m{ \A (?-s) (.+) (?s) .+ (?-s) (.+) (?s) .+ (?-s) (.+) \z }xms; " A: match, $1 'aa ' @ 0 $2 ' ' @ 9 $3 'c' @ 11
Give a man a fish: <%-{-{-{-<
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Recognizing 3 and 4 digit number
by kcott (Archbishop) on Jan 04, 2017 at 00:36 UTC | |
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 |