由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Unix版 - how to count string times using perl
相关主题
[转载] perl: regexhow to print the first paragraph of a text file?
请问怎么修改所有文件里面的字符串about strtok()
ASK AGAIN!!!Re: How to open/edit a huge text fileNeed urgent help on a samll shell script problem!
a question about regular expressionhow to find a file in unix
XEmacs C++-font-lock custumizing questionRe: 请推荐一本perl book好不好
regexp help好马配好鞍/介绍一套linux牛书/O'Reilly Animal Book
regex: what does [^ ] mean?shell programming
help on emacs / replaceCGI之Perl Script一问
相关话题的讨论汇总
话题: string话题: count话题: perl话题: using话题: times
进入Unix版参与讨论
1 (共1页)
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.
1 (共1页)
进入Unix版参与讨论
相关主题
CGI之Perl Script一问XEmacs C++-font-lock custumizing question
how to let awk not print?regexp help
how to write robort in perl w/out expect?regex: what does [^ ] mean?
How to do "which" in perl?help on emacs / replace
[转载] perl: regexhow to print the first paragraph of a text file?
请问怎么修改所有文件里面的字符串about strtok()
ASK AGAIN!!!Re: How to open/edit a huge text fileNeed urgent help on a samll shell script problem!
a question about regular expressionhow to find a file in unix
相关话题的讨论汇总
话题: string话题: count话题: perl话题: using话题: times