in reply to Validating a Regexp

sub is_valid_re { eval { qr/$_[0]/ }; return $@ ? 'NO' : 'YES'; } my @regexes = qw(a[hjg].* *a[hj); print is_valid_re($_), $/ for @regexes; __output__ YES NO
That seems to do the job.
HTH

_________
broquaint

Replies are listed 'Best First'.
Re: Re: Validating a Regexp
by Juerd (Abbot) on Jul 29, 2002 at 17:46 UTC

    sub is_valid_re { eval { qr/$_[0]/ }; return $@ ? 'NO' : 'YES'; }

    This eval returns a regex or undef, so you could write as follows:

    sub is_valid_re { eval { qr/$_[0] } ? 'YES' : 'NO' }
    But I'd _never_ return words when a boolean value is needed. Digits are so much nicer for that. If you want words, you could create yet another sub to turn the truth number into a word.
    sub is_valid_re { eval { qr/$_[0] } } print is_valid_re('abc') ? 'YES' : 'NO'; sub human_bool { shift ? 'YES' : 'NO' } print human_bool(is_valid_re('abc'));

    - Yes, I reinvent wheels.
    - Spam: Visit eurotraQ.