Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Debugging regular expressions

by larsen (Parson)
on Jun 21, 2001 at 00:57 UTC ( [id://90189]=perlmeditation: print w/replies, xml ) Need Help??

Today I had to write a regular expression definitely more complex than I'm used to do (I'm a real inept: for example this is the first time I use x modifier in REs :)).

This experience showed me I spent a lot of time trying to make my RE working, specially (in my opinion) 'cause I'm not able to debug it in a good way.

I used some tecniques in order to make my work faster (well, I should say "not so slow"...):

  • x modifier. I think the first debug trick is always avoid debug, i.e. write correct code :). And to write correct RE a good trick is comprehend what are you writing. Whitespaces and comments are very good and, last but not least, they let you...
  • proceed incrementally. I wrote my code incrementally, commenting large chunks of the regular expression and debugging only small pieces
I guess there are other tricks of ways to code better and faster (faster! faster than before... :)), like:
  • prepare case tests instead of using only actual data that will be processed by the RE.
  • exploit the isomorphism between RE and state machines. Did you ever simulated by hand your RE (I mean, a "real" RE) using paper, pencil, coins and drawing a state machine? (and using a computer?
  • I'd really like to discover there's a way to follow what's happening at "core level" when Perl try to satisfy a RE. This is not very good from my point of view (I think this kind of "insight" perturbs what's happening at core level), but I also think that such a possibility should save me some time this afternoon. Maybe something like this?

What do you think about?

Replies are listed 'Best First'.
Re: Debugging regular expressions
by wog (Curate) on Jun 21, 2001 at 01:13 UTC

    * I'd really like to discover there's a way to follow what's happening at "core level" when Perl try to satisfy a RE.

    use re 'debug';

    ... The re pragma can give you a hint (and possibly help you understand why you regexp doesn't do what you think it does). What it does output is not necessairly easy to understand.

    update: To address what bunsunsl said: this feature has been avialable since perl 5.005 and tests reveal it does not require the -DDEBUGGING to have been used when perl was compiled.

(Ovid) Re: Debugging regular expressions
by Ovid (Cardinal) on Jun 21, 2001 at 01:57 UTC
Re: Debugging regular expressions
by Abigail (Deacon) on Jun 21, 2001 at 04:22 UTC
    It also helps to "translate" the specification of how the to be parsed data looks like into a language that easily maps to regular expressions. As in:

    3 or 5 digits, a dash, 4 to 6 uppercase characters, a slash or whitespace, 2 digits and the letter K

    Once you have nailed down the specification, the implementation is often quite easy.

    -- Abigail

Re: Debugging regular expressions
by Albannach (Monsignor) on Jun 21, 2001 at 15:07 UTC
    I don't know why nobody has mentioned it yet, but I find YAPE::Regex::Explain (written by our own japhy) great for experimenting with different regex recipes, and certainly for deciphering what the masters have written, which speeds my learning process.

    --
    I'd like to be able to assign to an luser

Re: Debugging regular expressions
by clemburg (Curate) on Jun 21, 2001 at 12:35 UTC

    I think the first debug trick is always avoid debug, i.e. write correct code :). And to write correct RE a good trick is comprehend what are you writing.

    It also helps to "translate" the specification of how the to be parsed data looks like into a language that easily maps to regular expressions.

    A good technique to avoid errors and to code a regex according to specification is to build up the regex from strings stored in variables, which you then interpolate into the regex. This also makes the regex much more readable for others. Re: Trying to avoid line noise (brain cramp) gives an example.

    Christian Lemburg
    Brainbench MVP for Perl
    http://www.brainbench.com

Re: Debugging regular expressions
by busunsl (Vicar) on Jun 21, 2001 at 01:16 UTC
    With Perl 5.6 you can use 'use re "debug"' and will get something like a trace of the regex.
    It's not easy to read, but it helps.
    I think you must have perl compiled with the DEBUGGING flag.

    Look at perldoc perldebguts and search for 'regular'.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlmeditation [id://90189]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (5)
As of 2024-04-23 20:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found