Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > string matching specific number of times

Reply
Thread Tools

string matching specific number of times

 
 
tester
Guest
Posts: n/a
 
      09-29-2005
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
 
Reply With Quote
 
 
 
 
Paul Lalli
Guest
Posts: n/a
 
      09-29-2005
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";
}

Paul Lalli

 
Reply With Quote
 
 
 
 
Tad McClellan
Guest
Posts: n/a
 
      09-29-2005
tester <(E-Mail Removed)> wrote:

> 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";


--
Tad McClellan SGML consulting
http://www.velocityreviews.com/forums/(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
ced@carios2.ca.boeing.com
Guest
Posts: n/a
 
      09-29-2005

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

 
Reply With Quote
 
Dave Weaver
Guest
Posts: n/a
 
      09-30-2005
(E-Mail Removed) <(E-Mail Removed)> wrote:
>
> Or, simply:
>
> my $count = 0;
> $count++ while $something =~ /test/g;


Or save yourself the loop:

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

 
Reply With Quote
 
ced@carios2.ca.boeing.com
Guest
Posts: n/a
 
      09-30-2005

Dave Weaver wrote:
> (E-Mail Removed) <(E-Mail Removed)> wrote:
> >
> > Or, simply:
> >
> > my $count = 0;
> > $count++ while $something =~ /test/g;

>
> 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.

--
Charles DeRykus

 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      09-30-2005
tester <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> 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
--
If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Generate random string matching specific pattern and length Kevin Ruby 7 05-17-2011 02:47 AM
format a float number with specific number of decimal point Oscar Lok Ruby 1 11-19-2006 05:59 PM
compilation error: "error: no matching function for call to 'String::String(String)' =?ISO-8859-1?Q?Martin_J=F8rgensen?= C++ 5 05-06-2006 03:48 PM
Number of times number repeats viuxrluxvbbc@mailinator.com C++ 4 12-07-2005 07:46 PM
Get the number of times a URL- With query string it a server =?Utf-8?B?U3Jpbmk=?= ASP .Net 2 05-19-2004 12:54 PM



Advertisments