in reply to how to find substring using regex
Code tags are your (and our) friends.
Your problem is that you don't store the number of matches. The following code will fix your issue.
use warnings; use strict; my $cnt = 0; my $str = "hi hi harshmane hi hi"; $cnt = ($str =~ s/(harsh)/$1/g); print "'harsh' appears $cnt times.\n"; $cnt = ($str =~ s/(hi)/$1/g); print "'hi' appears $cnt times.\n"; #OUTPUT #'harsh' appears 1 times. #'hi' appears 4 times.