Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

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

by ikegami (Patriarch)
on Oct 08, 2009 at 19:17 UTC ( [id://800075]=note: print w/replies, xml ) Need Help??


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

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

Replies are listed 'Best First'.
Re^4: Regex for matching n-fold repititions of arbitrary characters
by pat_mc (Pilgrim) on Oct 08, 2009 at 19:49 UTC
    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?

      /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

      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)))
      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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://800075]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (4)
As of 2024-03-29 07:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found