Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
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 pondering the Monastery: (12)
As of 2014-12-17 22:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (36 votes), past polls