in reply to Issue with regex matching
Since $constant is user specified you could simply call quotemeta on the user input before using it. Then you won't need \Q and \E in your regex. E.g.:if($_ =~ m/^(\Q$constant\E)/) { $line_count++; }
You are currently only counting lines which start with that pattern. You wrote that you wanted to count all occurrences of the pattern. If you want to match all the occurrences of $constant per line you could change your code to:my $searchterm = quotemeta $user_input;
Other thoughts:while ($_ =~ m/\Q$constant\E/gc) { $count++; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Issue with regex matching
by AnomalousMonk (Archbishop) on Aug 29, 2013 at 00:46 UTC | |
|
Re^2: Issue with regex matching
by sowais (Sexton) on Aug 29, 2013 at 21:40 UTC |