Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Regex quantifiers composed?

by blakem (Monsignor)
on Oct 11, 2002 at 10:14 UTC ( [id://204458]=note: print w/replies, xml ) Need Help??


in reply to Regex quantifiers composed?

The question mark after a quantifier turns off greeding matching. In your regex it doesn't make much sense though. You're trying to match exactly 5 times, so greedy and non-greedy are the same thing.
/a{1,5}/; # match 1-5 'a's prefer the longest. /a{1,5}?/; # match 1-5 'a's prefer the shortest.
Here is an example:
#!/usr/bin/perl -wT use strict; $_ = 'aaaab'; # greedily match upto 5 'a's followed by a word char print "Greedy: "; print /(a{1,5})\w/, "\n"; # non-greedily match upto 5 'a's followed by a word char print "NonGreedy: "; print /(a{1,5}?)\w/, "\n"; __END__ Greedy: aaaa NonGreedy: a

-Blake

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (8)
As of 2024-04-24 12:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found