Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Re: Re: Re: A demanding parser

by TheDamian (Vicar)
on Jan 27, 2002 at 15:14 UTC ( [id://141925]=note: print w/replies, xml ) Need Help??


in reply to Re: Re: Re: A demanding parser
in thread A demanding parser

Ah, sorry. Missed that bit.

How about:

use Regexp::Common; print $RE{balanced}{-parens=>'()'}, "\n"; print $RE{balanced}{-parens=>'{}'}, "\n"; print $RE{balanced}{-parens=>'{}()'}, "\n"; # etc.
followed by reading the Regexp::Common source?

Damian

Replies are listed 'Best First'.
Re: A demanding parser (THE SOLUTION)
by gmax (Abbot) on Jan 31, 2002 at 06:59 UTC
    It works! I had some trouble figuring out how to implement it, because with 'use strict' the recursive RegEx stays undefined.
    #!/usr/bin/perl -w use strict; my $input = "aa (bb cc) dd (ee (ff (gg) hh) jj) kk ((ll)) mm"; no strict 'vars'; # from $RE{balanced}{-parens=>'()'}; $REparens = qr/ (?:(?:(?:[(](?:(?>[^)(]+) |(??{$REparens}))*[)])) |(?:(?!)))/x; my $re = $REparens; use strict; print "input: $input\n"; while ($input =~ /\G(\w+|$re)\s*/gc) { print "$1\n" } __END__ output: aa (bb cc) dd (ee (ff (gg) hh) jj) kk ((ll)) mm
    Making a block of no strict 'vars' solves the problem. There I can assign the recursive regex to a proper ly declared variable, or call explicitly ($main::REparens).
    Thanks a million.
     _  _ _  _  
    (_|| | |(_|><
     _|   
    

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2024-04-25 17:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found