Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re^2: String Matching

by frozenwithjoy (Priest)
on Aug 14, 2012 at 16:49 UTC ( #987411=note: print w/replies, xml ) Need Help??


in reply to Re: String Matching
in thread String Matching

Just for the record, the standard quantifiers in Perl regular expressions are, indeed, greedy.

Replies are listed 'Best First'.
Re^3: String Matching
by Jenda (Abbot) on Aug 15, 2012 at 07:51 UTC

    They are greedy, but not too clever

    my $str = "xaxaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; $str =~ /(a+)/; print "$1\n"; # prints "a" not "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
    The quantifiers are greedy, but they refuse to let go of something they found unless forced even if they could get more someplace later.

    Jenda
    Enoch was right!
    Enjoy the last years of Rome.

      Um, they start from the left, stop when they find a match -- that is clever
        And predictable! I'd hate to regex in a world that wasn't predictable.

      I'll see you and raise you.   /(a*)/ (note * quantifier in place of +) matches, but captures nothing, not even a single 'a'.

      >perl -wMstrict -le "my $s = 'xaxaaaaa'; print qq{matched, captured '$1'} if $s =~ /(a*)/; " matched, captured ''

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://987411]
help
Chatterbox?
[Corion]: Heh - I just realized, $work management just declared "no raises for anybody this year", and I'm not even angry about that. Even though that implicitly means "raises for the people with an automatic 5% raise in their contract", which the newly-merged ...
[Corion]: ... coworkers have. But I guess I've gone more mellow since I get to relax more, and such stuff doesn't make me as angry as it used to.
[Corion]: $boss will still get to listen to my interpretation :-D
[Eily]: hey, I'm just behind Larry in SioB \o/
[Corion]: Eily: Wheee ;)

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (6)
As of 2018-01-22 11:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How did you see in the new year?










    Results (233 votes). Check out past polls.

    Notices?