Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
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

Replies are listed 'Best First'.
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 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 LanX (Chancellor) 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?
[Corion]: ambrus: Details ;)
[ambrus]: Like <c>{ package AnyEvent::Impl:: Prima; use Prima; sub io{ my($s,%r)=@_; Prima::File->new( file=>$r{fh},mask =>("w"eq$r{poll}? fe::Write():fe:: Read())|fe:: Exception,onRead =>$r{cb},onWrite =>$r{cb}, onException=>$r{cb }) } sub timer { ... } push @AnyEvent::REGI
[ambrus]: argh, too long, let me try on scratchpad
[Corion]: . o O ( I seem to have improved my skills of getting other people to write code for me )

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (10)
As of 2016-12-08 12:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    On a regular basis, I'm most likely to spy upon:













    Results (141 votes). Check out past polls.