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,

sub re_valid_test { my $re = eval { qr/$_[0]/ }; defined($re) ? 1 : 0 ; }
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;

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
    Perl's native boolean false, as for example returned by defined, is never undef. Instead it is the dualvar value (numeric: 0, string: "").

    You can construct your own dualvar values using the function dualvar() from Scalar::Util. Fun for the whole family! :)

      Simpler than Scalar::Util's dualvar() would be to use the expression: !1.