Re: string matching specific number of times




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

.