Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Advance Regular expression questions

by sundialsvc4 (Monsignor)
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?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://946604]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (6)
As of 2014-09-17 06:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (61 votes), past polls