Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: Regex exact pattern match problem!

by eye (Chaplain)
on Nov 20, 2008 at 19:52 UTC ( #724969=note: print w/replies, xml ) Need Help??


in reply to Regex exact pattern match problem!

moritz's regex does work. Calling the replace operator (s///) once, produces the desired result. The solution fails because the replace operator is potentially called more than once by the while loop. I think the easiest solution is to remove the while loop.
#!/usr/bin/perl use strict; use warnings; my $toTranslate = "333333333333333333333333333333333333333333333333333 +333333333333333333333333333333333333333333333333333333333333333333233 +333333322333333333233333333333333333333333333333333323333333322333333 +332333333333"; $toTranslate =~ s/((?:3{4})+)/"[" . length($1) . "]"/ge; print $toTranslate,"\n";
The documentation notes that the global replace is not progressive like the match operator (in scalar context). That is only applicable to successive calls to the operators, not for the completion of a single call.

Replies are listed 'Best First'.
Re^2: Regex exact pattern match problem!
by johngg (Abbot) on Nov 20, 2008 at 23:16 UTC
    The solution fails because the replace operator is potentially called more than once by the while loop.

    The solution seems to work with or without the while, either way producing identical results. (I changed the 2s to 7s as the 2s were difficult to spot.)

    use strict; use warnings; my $toTranslate = q{3333333333773333377}; # 19 # .........1....1.... # ....5....0....5.... print qq{Sequences of 3s:-\n}; print qq{[@{ [ length $1 ] }]\n} while $toTranslate =~ m{(3+)}g; ( my $translated = $toTranslate ) =~ s{((?:3{4})+)}{ qq{[@{ [ length $1 ] }]} }eg; print qq{@{ [ q{-} x 50 ] }\n$translated\n@{ [ q{-} x 50 ] }\n}; 1 while $toTranslate =~ s{((?:3{4})+)}{ qq{[@{ [ length $1 ] }]} }eg; print qq{$toTranslate\n@{ [ q{=} x 50 ] }\n}; $toTranslate = q{3333333333333333333333333} # 25 . q{3333333333333333333333333} # 50 . q{3333333333333333333333333} # 75 . q{3333333333333333333333333} # 100 . q{3333333333333333373333333} # 125 . q{3377333333333733333333333} # 150 . q{3333333333333333333333733} # 175 . q{3333337733333333733333333} # 200 . q{3}; # 201 # .........1....1....2....2 # ....5....0....5....0....5 print qq{Sequences of 3s:-\n}; print qq{[@{ [ length $1 ] }]\n} while $toTranslate =~ m{(3+)}g; ( $translated = $toTranslate ) =~ s{((?:3{4})+)}{ qq{[@{ [ length $1 ] }]} }eg; print qq{@{ [ q{-} x 50 ] }\n$translated\n@{ [ q{-} x 50 ] }\n}; 1 while $toTranslate =~ s{((?:3{4})+)}{ qq{[@{ [ length $1 ] }]} }eg; print qq{$toTranslate\n@{ [ q{=} x 50 ] }\n};

    Produces

    Sequences of 3s:- [10] [5] -------------------------------------------------- [8]3377[4]377 -------------------------------------------------- [8]3377[4]377 ================================================== Sequences of 3s:- [117] [9] [9] [33] [8] [8] [9] -------------------------------------------------- [116]37[8]377[8]37[32]37[8]77[8]7[8]3 -------------------------------------------------- [116]37[8]377[8]37[32]37[8]77[8]7[8]3 ==================================================

    I hope this is of interest

    Cheers,

    JohnGG

      As I mentioned above, it doesn't work (unless you really want multiple passes) if the result of the first substitution itself contains 4 consecutive 3's (for example, if the original input has 33332 consecutive 3's). Also, to save yourself some typing, you may want to look at the documentation for the string repetition operator x.

      UPDATE: Thanks to johngg for pointing out that my documentation link was broken.

        Ah! I missed what you were driving at, having mis-read your input as '33332' not 33,332 '3's :-(

        You are quite correct, 1 while ... will make a mull of it in that case. You could keep the 1 while ... by adding the \G anchor at the beginning of the pattern but there's not really much point in doing that.

        use strict; use warnings; my $t1; my $t2; $t1 = $t2 = q{3} x 33335 . q{7}; print qq{Sequences of 3s:-\n}; print qq{>@{ [ length $1 ] }<\n} while $t1 =~ m{(3+)}g; print q{-} x 50, qq{\n}; 1 while $t1 =~ s{\G((?:3{4})+)}{ qq{[@{ [ length $1 ] }]} }eg; print qq{With 1 while and \\G\n$t1\n@{ [ q{-} x 50 ] }\n}; $t2 =~ s{((?:3{4})+)}{ qq{[@{ [ length $1 ] }]} }eg; print qq{Without either\n$t2\n@{ [ q{=} x 50 ] }\n};

        Produces

        Sequences of 3s:- >33335< -------------------------------------------------- With 1 while and \G [33332]3337 -------------------------------------------------- Without either [33332]3337 ==================================================

        I'm not sure what you mean regarding saving typing as I am using Multiplicative Operators (your link is slightly broken, BTW).

        Cheers,

        JohnGG

        Update: Further testing shows that the \G solution doesn't work with more than one group of 4 or more 3s.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://724969]
help
Chatterbox?
NodeReaper suddenly falls through the false ceiling

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2018-01-18 00:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How did you see in the new year?










    Results (206 votes). Check out past polls.

    Notices?