Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

Re^3: Regex exact pattern match problem!

by JadeNB (Chaplain)
on Nov 21, 2008 at 00:16 UTC ( #725030=note: print w/replies, xml ) Need Help??

in reply to Re^2: Regex exact pattern match problem!
in thread Regex exact pattern match problem!

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.

  • Comment on Re^3: Regex exact pattern match problem!

Replies are listed 'Best First'.
Re^4: Regex exact pattern match problem!
by johngg (Abbot) on Nov 21, 2008 at 13:19 UTC

    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};


    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).



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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://725030]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (9)
As of 2018-06-20 15:03 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (116 votes). Check out past polls.