Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: Limiting number of regex matches

by AnomalousMonk (Abbot)
on Sep 26, 2012 at 02:33 UTC ( #995674=note: print w/ replies, xml ) Need Help??


in reply to Limiting number of regex matches

Perhaps another way is with a counted quantifier (see Quantifiers in perlre):

>perl -wMstrict -le "my $mail = 'Spam mail sPam spAm stealthspam mail spaM'; print qq{you have the following mail: '$mail'}; ;; my $spam = qr{ (?i) \b spam \b }xms; for my $n (2 .. 5) { if (my ($spams) = $mail =~ m{ ((?: .*? $spam){$n}) }xms) { print qq{there are $n spam emails in mail: '$spams'}; } else { print qq{there are NOT $n spam emails in mail}; } } " you have the following mail: 'Spam mail sPam spAm stealthspam mail spa +M' there are 2 spam emails in mail: 'Spam mail sPam' there are 3 spam emails in mail: 'Spam mail sPam spAm' there are 4 spam emails in mail: 'Spam mail sPam spAm stealthspam mail + spaM' there are NOT 5 spam emails in mail


Comment on Re: Limiting number of regex matches
Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (5)
As of 2015-07-04 03:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (57 votes), past polls