If I want to use a user input string as a regular expression, what checks should I make? I think I only need to escape / and new line chars.
Obviously, it would be pointless to use quotemeta here :), but are there any other characters that I really should escape before using in eval?
Er, this is the sort of thing I was thinking of:
I *think* this is sound, but was wondering if anyone can drill any holes in it b4 I go any further down this route...$input = '...'; # input from user $value = '...'; # String I want to test to see if $input matches # escape s/delimiter/ $input =~ s|/|\/|gs; # strip new lines - not expected in input anyway $input =~ s/\n//gs; my $match; eval q{ $value =~ /($input)/; # try match $match = $1; # assign here because of local scope? }; if ($@ ne '') { # error because invalid reg exp sent } else { if ($match) { # regexp matched } else { # reg exp not matched } }
cLive ;-)
PS - perhaps I should wait and do this in Parrot instead? :)
In reply to Checking user input on dynamic regular expressions by cLive ;-)
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |