saranperl has asked for the wisdom of the Perl Monks concerning the following question:

This node falls below the community's minimum standard of quality and will not be displayed.

Replies are listed 'Best First'.
Re: count repeatation
by marto (Cardinal) on Aug 19, 2009 at 11:26 UTC

    "tell me the regex for that"

    This site isn't a code writing service. You've asked regex questions before and been advised to read perlre. Have you done so? Have you written any code for this problem yourself? If so what is it and what output did it give?

    Martin

      $string ='ab23cdefgXX(3A5)XXhijkl23mnXX(3)XXopq432rsXX(450b)XXtuv'; $count=0; while($string =~ m/XX/g) { $count++; } print $count;;
      I wrote it . I have given program above . i want to know the repeatation number without using while loop
Re: count repeatation
by biohisham (Priest) on Aug 19, 2009 at 15:58 UTC
    tell me the regex for that.......
    Hello, is this not being rude?. This community has a self-disciplined mutual cooperation agreement, we help each other out, we assist each other, but as we do take from each other we give back to each other.
    I am not offensive when I say the way you put this question sounds like we "are down at your service your highness", sorry to everybody who thinks this is rude on my part, but saranperl, you have been advised on many issues many a times and you have been given some very valuable feedback, very valuable links and above all, very valuable time, voluntarily, and still you don't sweat on it to make a difference you want things to be ready served to you on a plate!!!, That is lazy, selfish and inconsiderate of yours to take us as ladders for your climb, I wish you luck anyways but...


    Excellence is an Endeavor of Persistence. Chance Favors a Prepared Mind.
Re: count repeatation
by JavaFan (Canon) on Aug 19, 2009 at 11:31 UTC
    There's no 'xx' in your string, so the answer is 0.
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: count repeatation
by Marshall (Canon) on Aug 19, 2009 at 14:33 UTC
    The tr =~ trick won't work because you have multiple letters in your pattern.

    #!/usr/bin/perl -w use strict; my $string ='ab23cdefgXX(3A5)XXhijkl23mnXX(3)XXopq432rsXX(450b)XXtuv'; my $num = (my @matches) = ($string =~ m/xx/ig); print $num; __END__ prints 6
      And use of the goatse 'operator'  =()= eliminates the need for a possibly redundant lexical array variable (matching in this example is case-sensitive):
      >perl -wMstrict -le "my $str ='ab2cgXX(A5)XXhl3mnXX(3)XXo42rsXX(4b)XXv_xx'; my $reps =()= $str =~ m{ XX }xmsg; print $reps; " 6
        Interesting...Never heard the term goatse before! I don't know that the lexical variable is "redundant". I figure that Perl creates it whether it has a name or not! Adding a lexical name to this goatse list appears to be just a minor detail. It could be that there are even shorter ways to formulate this function. I just wrote the first 2 obvious lines of code that came to mind.

        This was not a difficult problem and quite frankly, I would expect the OP to spend more time thinking about it.

        Aargh! I had almost forgotten. Damn you!

        Just a something something...
      Yes and no. You can still use the tr/// trick by using s///.
      my $num = $string =~ s/XX/XX/g;

      It works in list context too.

      print $string =~ s/XX/XX/g;
        And if XX contains regexp meta characters, only can write:
        s/(XX)/$1/g;
Re: count repeatation
by bichonfrise74 (Vicar) on Aug 19, 2009 at 16:06 UTC
    There are more than one way to do this.
    #!/usr/bin/perl use strict; my $string = 'ab23cdefgXX(3A5)XXhijkl23mnXX(3)XXopq432rsXX(450b)XXtuv' +; my @counts = split( "XX", $string ); print $#counts;
      That doesn't always provide the correct results.
      my $string = "XXXX"; my @count = split( "XX", $string ); print $#count; # Prints -1
      One could fix that by giving split third argument.

      But it doesn't scale to generic patterns. Suppose you want to count how after characters are repeated (so, count not only "XX", but also "AA", "22", "--", etc). A pattern /(.)\1/ matches that, but

      my $string = "XXYY"; my @count = split( /(.)\1/, $string, -1 ); print $#count; # Prints 4, not 2.
      The reason is that if the pattern in contains capturing parenthesis, split returns what's captured in the parens as well.
Re: count repeatation
by sanku (Beadle) on Aug 21, 2009 at 12:04 UTC
    hi, This regular expression will work fine.
    $string ='ab23cdefgXX(3A5)XXhijkl23mnXX(3)XXopq432rsXX(450b)XXtuv'; @length= $string =~ m/XX/g; print scalar @length;