gri6507 has asked for the wisdom of the Perl Monks concerning the following question:
I have a bit of a dillema. I have a need to take user input and use it as the regexp pattern in the search against some data. Here's an example code:
use warnings; use strict; print "Input regexp: \n"; my $user_input = <STDIN>; my $string = "abcdef"; if ($string =~ /$user_input/x) { print "$string - matches\n"; } else { print "$string - no match found\n"; }
Quantifier follows nothing in regex; marked by <-- HERE in m/* <-- HERE aI understand why this error happens, but I would like to trap it in a more user friendly way. Is there a method for ensuring that a regexp is valid before passing it to the m// operator?
Thank you as always!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Making sure user input is a valid regexp
by moritz (Cardinal) on Jun 08, 2010 at 12:54 UTC | |
by ReturnOfThelonious (Beadle) on Jun 08, 2010 at 14:43 UTC | |
by choroba (Cardinal) on Jun 08, 2010 at 14:49 UTC | |
|
Re: Making sure user input is a valid regexp
by ww (Archbishop) on Jun 09, 2010 at 01:29 UTC | |
|
Re: Making sure user input is a valid regexp
by rjt (Curate) on Jun 08, 2010 at 17:43 UTC |