Scripting >> Perl >> Regular Expressions >> How to count number of occurrence of a character

print CountMatch("bc","|") . "\n"; print CountMatch("|bc","|") . "\n"; print CountMatch("||bc","|") . "\n"; print CountMatch("|||||bc","|") . "\n"; sub CountMatch() { $s_string = shift; $s_pattern = shift; $count=0; $s_string =~ s/\|/\|/gi; $s_pattern =~ s/\|/\|/gi; while ($s_string =~ /$s_pattern/g) { $count++ } return $count; }