Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: Nesting regexen

by nothingmuch (Priest)
on Jul 11, 2005 at 07:25 UTC ( [id://473855]=note: print w/replies, xml ) Need Help??


in reply to Nesting regexen

Regular expressions are not parsers.

A regular expression can refer to itself, but that is a kludgy hack. To do that use the ??{ } form inside a regex.

There are better solutions though.

If you want to clean up your perl code, perltidy helps. If you want to do custom transformations, PPI is well suited for the job.

Larry Wall is working on the perl 5 to perl 6 converter, and he's using the parsing code from perl 5 itself to emit a canonical format... When he makes a release that might be useful.

If you want to parse it on your own, try looking at Parse::RecDescent.

Back to your problem - since perl's grammar is recursive: (... ( sub expression ) ...) you have to keep track of parenthesis balancing. You need to use some kind of stack structure (an explicit one or the call stack) to nibble paren tokens, and construct nested subexpressions. Once you can build that much you need to reserialize your structures back. Since you are only going two levels deep, this will be a problem soon.

You can use regexes to find tokens, but the notion of state must be maintained, and this is not taken into account in your code.

Your subroutine regex is the problem there, btw. It doesn't match a closing paren, so the collected string ends in "WebGUIProfile", and stops there. A hard coded paren for the method call is printed, and that's where it ends.

-nuffin
zz zZ Z Z #!perl

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (2)
As of 2024-04-26 00:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found