Re: string matching specific number of times
- From: "ced@xxxxxxxxxxxxxxxxxxxxx" <ced@xxxxxxxxxxxxxxxxxxxxx>
- Date: 29 Sep 2005 11:17:38 -0700
Paul Lalli wrote:
> tester wrote:
>
> > My CPU taking 100% if I try to match more than the existing number of
> > specific matches in a string. Script is ...
> >
> > my $something = "this is a test, just test, no kidding, small test, but
> > error in test. over over";
> > if ($something =~ m/((.*)test(.*)){5}/){
> > print "number of matches are 5, successful";
>
> Instead of forcing the regexp engine took look for all the .*
> repeatedly, why not just determine how many instances of 'test' there
> are, and compare that to the number you want?
>
> my @tests = $something =~ /test/g;
> if (@tests == 5){
> print "Success\n";
> } else {
> print "Failed, found " . @tests . " copies of 'test'\n";
> }
>
Or, simply:
my $count = 0;
$count++ while $something =~ /test/g;
....
else {
print "Failed, found $count copies of 'test'\n";
--
Charles DeRykus
--
Charles DeRykus
.
- References:
- string matching specific number of times
- From: tester
- Re: string matching specific number of times
- From: Paul Lalli
- string matching specific number of times
- Prev by Date: Re: regex replace credit card numbers with *
- Next by Date: Re: XML Parsing
- Previous by thread: Re: string matching specific number of times
- Next by thread: Re: string matching specific number of times
- Index(es):