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


In reply to Re: Test RegEx Validity by Zaxo
in thread Test RegEx Validity by aplonis

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.