string matching specific number of times

T

tester

Hi All,

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";
}
else{
print "no 5 matches, failure";
}

String "test" is repeated only 4 times in $something but if I am trying
to check whether the $something has 5, my CPU taking 100%. What is the
issue?

Thanks,
tester
 
P

Paul Lalli

tester said:
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";
}

Paul Lalli
 
T

Tad McClellan

tester said:
if ($something =~ m/((.*)test(.*)){5}/){
print "number of matches are 5, successful";


To be accurate, that should be:

print "number of matches is at least 5, successful";
 
C

ced

Paul said:
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";
 
C

ced

Dave said:
Or save yourself the loop:

my $count = () = $something =~ /test/g;


There's still a loop returning values which are just discarded
by the empty list of course. Losing the counter init./increm.
is very cool though.
 
A

Anno Siegel

tester said:
Hi All,

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";
}
else{
print "no 5 matches, failure";
}

String "test" is repeated only 4 times in $something but if I am trying
to check whether the $something has 5, my CPU taking 100%. What is the
issue?

Probably excessive backtracking. Lots of alternatives have already been
proposed.

Anno
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,539
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top