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
| [reply] |
|
|
$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 | [reply] [d/l] |
|
|
| [reply] |
|
|
Re: count repeatation
by biohisham (Priest) on Aug 19, 2009 at 15:58 UTC
|
| [reply] [d/l] |
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. | [reply] |
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
| [reply] [d/l] |
|
|
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
| [reply] [d/l] [select] |
|
|
| [reply] |
|
|
|
|
|
|
|
|
|
|
| [reply] |
|
|
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;
| [reply] [d/l] [select] |
|
|
And if XX contains regexp meta characters, only can write:
s/(XX)/$1/g;
| [reply] [d/l] |
|
|
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;
| [reply] [d/l] |
|
|
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. | [reply] [d/l] [select] |
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;
| [reply] [d/l] |