Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

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

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


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

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?


Comment on Re^4: Regex for matching n-fold repititions of arbitrary characters
Download Code
Re^5: Regex for matching n-fold repititions of arbitrary characters
by bv (Friar) on Oct 08, 2009 at 20:21 UTC

    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)))
Re^5: Regex for matching n-fold repititions of arbitrary characters
by ikegami (Pope) on Oct 08, 2009 at 21:17 UTC

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

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

      Not necessarily! It makes sense when parsing with nested ifs! Friedl's book has some nifty examples...

      Cheers Rolf

        I didn't say m//g in scalar context outside a loop is wrong. I said it's usually wrong. I said the code he used was wrong.

        And I bet his tokenizers used /c too

Re^5: Regex for matching n-fold repititions of arbitrary characters
by LanX (Canon) on Oct 08, 2009 at 21:28 UTC
    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://800080]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (14)
As of 2014-11-28 13:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (197 votes), past polls