f**g 发帖数: 28 | 1 Hi all,
Any one can give me some suggestion?
I try to count string times within a word using perl. Such as
how many times of "AB" can be found in string
ABCDDDEEDDABDDEAAB..
Many thanks. |
d****p 发帖数: 685 | 2 Use RegExp.
You may try the following code:
my $string = "ABCDDDEEDDABDDEAAB";
my $count = 0;
while ($string =~ s/AB//) {$count++}
print $count; |
t*****g 发帖数: 1275 | 3 perl -e 'print scalar split /AB/, "ABCDDDEEDDABDDEAAB";'
【在 f**g 的大作中提到】 : Hi all, : Any one can give me some suggestion? : I try to count string times within a word using perl. Such as : how many times of "AB" can be found in string : ABCDDDEEDDABDDEAAB.. : Many thanks.
|
d****p 发帖数: 685 | 4 Using split generally is clean but not robust in this situation. Try "
ABABABAB": it returns 0.
This is because split functions treats the leading and trailing delimiters
differently. It ignores all trailing delimiters while insert an empty string
before the leading delimiter. |