Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re^2: Regex for matching n-fold repititions of arbitrary characters

by pat_mc (Pilgrim)
on Oct 08, 2009 at 19:15 UTC ( #800073=note: print w/ replies, xml ) Need Help??


in reply to Re: Regex for matching n-fold repititions of arbitrary characters
in thread Regex for matching n-fold repititions of arbitrary characters

Thanks, ikegami ... I knew there should be an easy way to do it ... but had not used backreferences in regexes before.

One follow-up to your post: Is the s modifier really required?

Thanks again!

Pat


Comment on Re^2: Regex for matching n-fold repititions of arbitrary characters
Download Code
Re^3: Regex for matching n-fold repititions of arbitrary characters
by ikegami (Pope) on Oct 08, 2009 at 19:17 UTC
    Depends on what you want /./ to match. If you want to match repetitions of any character, yes. If you want to match repetitions of specific characters, no, since you won't be using /./.
      Hm ... I follow you so far, ikegami ... but why do I get different output in the following two cases:
      ~$ perl -le 'print $& if "aabbaab" =~ /aa|bb/g' ~$ perl -le 'print join "\n", ( "aabbaab" =~ /aa|bb/g )' aa bb aa ~$ perl -le 'print join "\n", ( "aabbaab" =~ /(.)\1/g )' a b a

      The first two system responses are clear ... but why does the last regex using backreferences return only single characters?

        Because you are only capturing one character. m// in list context returns the list of captures, which in your case is the (.). The backreference is not captured.

        print pack("A25",pack("V*",map{1919242272+$_}(34481450,-49737472,6228,0,-285028276,6979,-1380265972)))

        /pat/g means /(pat)/g if pat contains no captures. In list context, m// returns what it captured (or 1 if there are no captures) on success.

        $ perl -wle'print for "aabbaab" =~ /((.)\2+)/sg;' aa a bb b aa a
        $ perl -wle'print $1 while "aabbaab" =~ /((.)\2+)/sg;' aa bb aa

        Note that if "aabbaab" =~ /aa|bb/g is wrong. Red flags should be raised when you see /g in scalar context outside a loop.

        maybe that's clearer:

        $ perl -e 'print $1,$2,"\n" while ( "aabbaab" =~ /(.)(\1)/g )' aa bb aa
        or
        $ perl -e 'print $&,"\n" while ( "aabbaab" =~ /(.)\1/g )' aa bb aa

        Cheers Rolf

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://800073]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (9)
As of 2014-10-23 07:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (124 votes), past polls