Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Regex quantifiers composed?

by blakem (Monsignor)
on Oct 11, 2002 at 10:14 UTC ( #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
Node Status?
node history
Node Type: note [id://204458]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (6)
As of 2019-08-23 15:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?