Beefy Boxes and Bandwidth Generously Provided by pair Networks Cowboy Neal with Hat
more useful options
 
PerlMonks  

Re: Validating a Regexp

by broquaint (Abbot)
on Jul 29, 2002 at 10:41 UTC ( [id://185996]=note: print w/replies, xml ) Need Help??

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.


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 13: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.
    

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://185996]
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.