三个方法如下:
#!/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";
温馨提示:答案为网友推荐,仅供参考