Re: string matching specific number of times



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

.



Relevant Pages

  • Re: string matching specific number of times
    ... > My CPU taking 100% if I try to match more than the existing number of ... > specific matches in a string. ... Script is ... ... "Reply" at the bottom of the article headers. ...
    (comp.lang.perl.misc)
  • string matching specific number of times
    ... My CPU taking 100% if I try to match more than the existing number of specific matches in a string. ... Script is ... ...
    (comp.lang.perl.misc)
  • Re: How to rewrite with awk?
    ... > I'm unfamiliar with tools such as sed & awk. ... Extract the string that matches a RE. ... This script will not only expand all the lines that say "include ... file) and not resetting ARGV(the tmp file), it then lets awk do any ...
    (comp.unix.shell)
  • Re: Function that returns date of file.
    ... string after the date/time when it is used by itself. ... Is that your entire script? ... I make an IF statement that required the 'equals equals'. ... designed database your job will be all that much harder. ...
    (alt.php)
  • Re: DBI Performance Issues
    ... use the cpu or the network hardware during remote tests. ... first the live script is run from a sun solaris machine using ... server machine. ...
    (comp.lang.perl.misc)