急问如何利用perl统计一行字符串里的指定字符的个数 65
用的是perl,想用正则表达式写。譬如给的字符串是abcsfsaf#sdagasdga#sdgaghoiiopjh#dsfjkopahkl;fjh#dsjfklpafj我...
用的是perl,想用正则表达式写。譬如给的字符串是abcsfsaf#sdagasdga#sdgaghoiiopjh#dsfjkopa hkl;fjh#dsjfklpafj
我希望的结果是统计出#号有4个。求高手帮忙 展开
我希望的结果是统计出#号有4个。求高手帮忙 展开
5个回答
展开全部
遍历统计好了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
#!/usr/bin/perl -w
use strict;
my $str = "abcsfsaf#sdagasdga#sdgaghoiiopjh#dsfjkopa hkl;fjh#dsjfklpafj";
my $count = 0;
while( $str =~ /#/g ) {
$count++;
}
print "num of # is: $count\n";
要统计其他字符,把pattern替换即可了
use strict;
my $str = "abcsfsaf#sdagasdga#sdgaghoiiopjh#dsfjkopa hkl;fjh#dsjfklpafj";
my $count = 0;
while( $str =~ /#/g ) {
$count++;
}
print "num of # is: $count\n";
要统计其他字符,把pattern替换即可了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
perl -e '$a="abcsfsaf#sdagasdga#sdgaghoiiopjh#dsfjkopa hkl;fjh#dsjfklpafj"; print $a=~s/#/$&/g;'
结果是4
结果是4
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
三个方法如下:
#!/bin/perl
-w
use
strict;
my
$str
=
"abcsfsaf#sdagasdga#sdgaghoiiopjh#dsfjkopa
hkl;fjh#dsjfklpafj";
###########
方法一
my
$pos
=
-1;
my
$num
=
0;
while
(($pos
=
index($str,
"#",
$pos))
>
-1)
{
print
"Found
at
$pos\n";
$pos++;
$num
++;
}
print
"找到$num个!\n";
###############################################################
#
方法二
my
$count
=
0;
while(
$str
=~
/#/g
)
{
$count
++;
}
print
$count,"\n";
##########################################
#
方法3
my
$cc
=
0;
my
$tmp
=
0;
if(
$tmp
=
()
=
($str
=~
/#/g
)
)
{
$cc
+=
$tmp;
}
print
"$cc\n";
#!/bin/perl
-w
use
strict;
my
$str
=
"abcsfsaf#sdagasdga#sdgaghoiiopjh#dsfjkopa
hkl;fjh#dsjfklpafj";
###########
方法一
my
$pos
=
-1;
my
$num
=
0;
while
(($pos
=
index($str,
"#",
$pos))
>
-1)
{
"Found
at
$pos\n";
$pos++;
$num
++;
}
"找到$num个!\n";
###############################################################
#
方法二
my
$count
=
0;
while(
$str
=~
/#/g
)
{
$count
++;
}
$count,"\n";
##########################################
#
方法3
my
$cc
=
0;
my
$tmp
=
0;
if(
$tmp
=
()
=
($str
=~
/#/g
)
)
{
$cc
+=
$tmp;
}
"$cc\n";
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询