Hi,
I have some code that accepts a string from a user to use as a regular expression in a filter. (E.g if they want the script to output a list of items beginning abc they would enter ^abc .)
This bit works well when they enter a valid regular expression. However I'm struggling with a way of validating the regexp prior to using it (ie make sure they've entered a valid regexp, not worried at this stage what it matches just that it's a valid string). The best I've come up with it
use strict; use warnings; print validate('a*') . "\n"; print validate('*') . "\n"; sub validate{ my $pat = shift; if (eval{qr/$pat/}){ return 1; }else{ return 0; } }
This works but feels like a rather ugly way to do this. Can anyone think of a better way?
I can't just use \Q and \E, I want them to be able to enter meta characters.
Thanks
Graham
In reply to Validating Regular Expression by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |