Subject: | Suggested policy: forbid .* at start or end of unanchored regexp |
Beginning perl programmers will sometimes write regexp tests like:
if ($string =~ /.*(\d+).*/) {
# do something with $1
}
The intention is to match a number somewhere in the input string. But
because the regexp engine tries all possible start positions anyway, the
initial .* is redundant, and since the regexp engine ignores unmatching
stuff at the end, the final .* is also redundant.
I suggest that code like this indicates some confusion about how perl's
regular expressions work, and a warning is very worthwhile to explain to
the programmer his or her mistake.
A policy should warn about .* at the very start or end of a regexp used
for m// matching (but not for s///). The warning text should suggest
using either
/\A(\d+)\z/ to match the entire string, or
/(\d+)/ to search the string and find a match at any point.