Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Test regex compile

by scottb (Scribe)
on May 13, 2004 at 17:38 UTC ( #353160=perlquestion: print w/replies, xml ) Need Help??
scottb has asked for the wisdom of the Perl Monks concerning the following question:


Is there a way that I can test whether or not a regular expression will compile without error without actually compiling it (and dieing if it does fail)?

I have a tool I have written where my coworker and I must be adding and removing regular expressions on the fly; as it stands if we make a typo in the regex, the program dies which is highly undesireable.

Thank you,

Replies are listed 'Best First'.
Re: Test regex compile
by pbeckingham (Parson) on May 13, 2004 at 17:52 UTC

    You could eval{qr /something/};.

      Thanks pbeckingham, this was exactly the answer I needed. The key here was being able to test *on the fly*, which this accomplishes well.
Re: Test regex compile
by McMahon (Chaplain) on May 13, 2004 at 19:48 UTC

    I haven't used it yet, but the jEdit editor has plugin called "Regular Expression Tester". I like jEdit, this might do what you need.

    jEdit also has a "PerlBrowse" plugin that might also be handy in your situation.
Re: Test regex compile
by nimdokk (Vicar) on May 13, 2004 at 19:27 UTC
    What I sometimes do when working with a regex is to isolate it out into a test script that will just test the working of the regex so I will know quite quickly if it is doing what I expect.
Re: Test regex compile
by TomDLux (Vicar) on May 13, 2004 at 18:10 UTC

    Let's re-phrase the question to: Is there a way that I can test prior to running a progrgam whether or not a regular expression will compile.

    The answer is Yes, use the -c flag when invoking perl. You can use: Perl -c myprogram, or you might make the program read in the regex from an external file ... possibly specifying which file on the command line. Then you only have to test the regex file: Perl -c regexfile.


      That doesn't work if the regex is passed in as a parameter, say if you are doing something screwy like loading it from a database. The way to do that is probably to check at startup, or otherwise write another program, to run eval-block type tests on each of the many regexes that can be imported.
Re: Test regex compile
by SavannahLion (Pilgrim) on May 14, 2004 at 02:51 UTC
    I'm just asking for two cents here, since I don't really know the full circumstances of this situation. But I'm writing because someone mentioned accepting regexs through a database.

    In a worst case scenario, isn't this kind of asking for trouble? What if someone wanted to insert malicious code into this running script? Granted, I really don't know the full implications of using an Eval block to run unknown code, but wouldn't using Reval be a bit more desirable with something like this? I'm just thinking in terms of reducing the amount of risk present to a computer.

    If I'm way off base here, I'd appreciate some clarification on this. Whenever I thought I had an excuse to use an Eval, I always found a much better way through refactoring. :)

    Thanks for your patience.
    Prove your knowledge @ HLPD

      Agreed. Running eval on untrusted input is a recipe for disaster!

      Using Taint would be a very good idea, although I'm not sure how you'd make a regex that correctly extracts a regex :-)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://353160]
Approved by pbeckingham
Front-paged by pbeckingham
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2018-08-18 05:01 GMT
Find Nodes?
    Voting Booth?
    Asked to put a square peg in a round hole, I would:

    Results (185 votes). Check out past polls.