Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Re: A demanding parser

by TheDamian (Priest)
on Jan 25, 2002 at 17:12 UTC ( #141489=note: print w/replies, xml ) Need Help??


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

Is there any way of avoiding the external module and catch an arbitrary number of nested parentheses with a "normal" Regex?
use Regexp::Common; $str =~ /$RE{balanced}{-parens=>'()'}/

Replies are listed 'Best First'.
Re: Re: Re: A demanding parser
by Juerd (Abbot) on Jan 25, 2002 at 20:34 UTC
    That's what he does already. He asked if it were possible without the module. (Not a very good idea, but I don't know the motivation (education, perhaps))

    2;0 juerd@ouranos:~$ perl -e'undef christmas' Segmentation fault 2;139 juerd@ouranos:~$

      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

        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
Node Status?
node history
Node Type: note [id://141489]
help
Chatterbox?
[TStanley]: I'm sitting in the airport in Tampa, Florida at the moment, getting ready to head home.
[talexb]: TStanley I'm flying to Vegas in two weeks time. After that flights leaving Vegas and Phoenix are late at night .. I've been hearing that it gets so hot that the air isn't dense enough for planes to take off. Weird. Guess you won't have that problem.
[Corion]: Yay, vacation time now - one week off work (but a Perl workshop in the middle ;)) )
[talexb]: TStanley I just realized that I was in your area for a convention back in April. We were at the high school for the weekend, the one with the long driveway. Nice spot.

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (11)
As of 2017-06-23 14:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How many monitors do you use while coding?















    Results (548 votes). Check out past polls.