Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

Re: Advance Regular expression questions

by sundialsvc4 (Abbot)
on Jan 06, 2012 at 14:33 UTC ( #946604=note: print w/replies, xml ) Need Help??

in reply to Advance Regular expression questions

For tasks like these I often pull out bigger guns, like Parse::RecDescent, which not only does a lot of the regular-expression work for you but also sets the whole thing up in a hierarchical way, and the parser includes a certain amount of built-in “find a way to get there” capability.

To illustrate this point of view, the file appears to consist, at the highest level, of a list of zero-or-more groups, each enclosed by parentheses and each group separated from the next by one comma.   That is the outermost-level description of the file.   The first “inner” description is to say that each group consists of a list of one or more “tokens” separated by commas, where a “token” is either a number or a quoted-string.

Where parsers really start to shine, though, is when you might want to express some sort of rule about, say, the structure of (or, the meaning of...) one of those groups, especially if the structure of a group may vary in some way.   A parser takes as its basic input a formal description of (a grammar for...) what a valid file may consist of, not just physically but structurally, and it seeks to match what it is given to whatever the grammar says that it is to expect.   Exactly like finding your way through an unfamiliar city using a map.

In my experience, messy file-parsing tasks (done without a parser/grammar) can very quickly devolve into “write-only code.”   You might establish that it works correctly now, but you dare not touch it again.   In many shops, exactly such programs can be found in abundance:   mission-critical, and utterly fossilized.

  • Comment on Re: Advance Regular expression questions

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://946604]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (10)
As of 2017-10-18 17:48 GMT
Find Nodes?
    Voting Booth?
    My fridge is mostly full of:

    Results (250 votes). Check out past polls.