string matching specific number of times

Discussion in 'Perl Misc' started by tester, Sep 29, 2005.

  1. tester

    tester Guest

    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
    tester, Sep 29, 2005
    #1
    1. Advertising

  2. tester

    Paul Lalli Guest

    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
    Paul Lalli, Sep 29, 2005
    #2
    1. Advertising

  3. tester <> 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
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Sep 29, 2005
    #3
  4. tester

    Guest

    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
    , Sep 29, 2005
    #4
  5. tester

    Dave Weaver Guest

    <> wrote:
    >
    > Or, simply:
    >
    > my $count = 0;
    > $count++ while $something =~ /test/g;


    Or save yourself the loop:

    my $count = () = $something =~ /test/g;
    Dave Weaver, Sep 30, 2005
    #5
  6. tester

    Guest

    Dave Weaver wrote:
    > <> 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
    , Sep 30, 2005
    #6
  7. tester

    Anno Siegel Guest

    tester <> 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.
    Anno Siegel, Sep 30, 2005
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?bWF2cmlja18xMDE=?=

    SetAuthCookie works some times and fails some times?

    =?Utf-8?B?bWF2cmlja18xMDE=?=, Mar 23, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    512
    =?Utf-8?B?bWF2cmlja18xMDE=?=
    Mar 23, 2006
  2. =?Utf-8?B?bWF2cmlja18xMDE=?=

    Forms Authentication Fails some times and not some times???

    =?Utf-8?B?bWF2cmlja18xMDE=?=, Mar 28, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    488
    =?Utf-8?B?bWF2cmlja18xMDE=?=
    Mar 28, 2006
  3. djskrill
    Replies:
    9
    Views:
    699
    djskrill
    Oct 1, 2003
  4. Replies:
    4
    Views:
    305
    Neil Cerutti
    Dec 7, 2005
  5. Kevin
    Replies:
    7
    Views:
    181
    Kevin
    May 17, 2011
Loading...

Share This Page