waspcatcher has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I am parsing input from a CGI form, and so want to run everything through regular expressions to untaint the data.

Yesterday, I grabbed the data ($data = $form->param('input') )and sent it through a reg exp ( $data =~ /(^[\w-.\s\(\)\']+)$/ ) and whenever metacharacters were part of $data, it would knock Perl over.

Then I used $data = "\Q$data\E" before putting it through the regexp, which worked fine.

Today I carry on working, and suddenly the regexp was complaining about everything. I took a look at what was happening, and it was effectively being escaped twice. Perl wasn't complaining (I'm using strict, warnings, CGI qw(:standard) and CGI::Carp qw/fatalsToBrowser/.

It's working now, but I don't know what would make illegal characters knock it over yesterday and not today.

Any info would be greatly appreciated.

waspcatcher

Edit Masem 2001-11-08 - CODE tag on regex.

Replies are listed 'Best First'.
Re: metacharacters and reg exps
by waspcatcher (Initiate) on Nov 09, 2001 at 18:39 UTC
    I found my answer by not using the \Q and \E but rather using quotemeta() - the only reason that I used \Q\E ws that it was quoted in the Perl Cookbook as an alternative, and did not think that it was that which was at fault. cheers waspcatcher