Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!swrinde!elroy.jpl.nasa.gov!ncar!gatech!usenet.ins.cwru.edu!ncoast!allbery From: allbery@NCoast.ORG (Brandon S. Allbery KB8JRR/AA) Newsgroups: comp.lang.perl Subject: Re: Perl code to test for valid reg-exp? Message-ID: <1991May2.232320.12037@NCoast.ORG> Date: 2 May 91 23:23:20 GMT References: <9105021740.AA06244@peanut.crl.dec.com> Reply-To: allbery@ncoast.ORG (Brandon S. Allbery KB8JRR/AA) Followup-To: comp.lang.perl Organization: North Coast Public Access Un*x (ncoast) Lines: 23 As quoted from <9105021740.AA06244@peanut.crl.dec.com> by jmiller@crl.dec.com: +--------------- | Has anyone got a short (preferably) piece of Perl code to test a | string to see whether or not it is a valid regular expression for use | in Perl? I've written a program that allows users to type a string | and then uses that to match against a data base; unfortunately the | program blows up if the user types in an invalid string! +--------------- Well, the trivial way is: eval '/$trial_regexp/; 1' || print "Invalid regexp: $@"; I.e. let the "eval" run it. If the eval bombs, it will return 0, else 1 (because we told it to). The error message that would have splattered onto the terminal is in $@. ++Brandon -- Me: Brandon S. Allbery Ham: KB8JRR/AA 10m,6m,2m,220,440,1.2 Internet: allbery@NCoast.ORG (restricted HF at present) Delphi: ALLBERY AMPR: kb8jrr.AmPR.ORG [44.70.4.88] uunet!usenet.ins.cwru.edu!ncoast!allbery KB8JRR @ WA8BXN.OH