Your code would increment $cnt once if the expression is true (matches one or more times). You could turn it around like this:
$cnt++ while $str=~/harsh/g;
Note also that you don't need to store the matches (with parentheses) for what you are doing.
And the '+' modifier in your example would match 'harsh' and 'harshhhhhhhhhh'. Is that what you meant?
In reply to Re: how to find substring using regex
by hbm
in thread how to find substring using regex
by harshmane
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |