in reply to Test RegEx Validity
See warnings below, but you can test validity of a string as a regex without running a match on it. You can do that by eval-ing the qr// operator,
If you're willing to accept undef instead of zero as false, you don't need the trinary. Just leave the last line as defined $re;sub re_valid_test { my $re = eval { qr/$_[0]/ }; defined($re) ? 1 : 0 ; }
Your less amiable users may enjoy handing you regexen containing containing (?{...}) and (??{...}) constructs. You might not enjoy it so much. You give users the ability to execute arbitrary perl code. Of course for shell users that is the case anyway, but this would be very bad in a suEXEC'd web app.
You need to test ought-to-be-tainted input for safety as well as validity. That is not easy. You need a parser for regular expressions. That will test validity as well as giving you a chance to reject code constructs.
After Compline,
Zaxo
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Test RegEx Validity
by bart (Canon) on Nov 28, 2004 at 03:02 UTC | |
by benizi (Hermit) on Dec 07, 2004 at 18:49 UTC |