eg8rds has asked for the wisdom of the Perl Monks concerning the following question:

Is there an easy way of saying "matching bracket" in my regular expressions. For example, if I wanted to get slurp in a c file, and put each of the functions into an array, can I match the "void xxx (void){" and then say "until the matching bracket"? Cheers guys.

Replies are listed 'Best First'.
Re: pattern matching with brackets
by blazar (Canon) on Jul 12, 2005 at 09:00 UTC
Re: pattern matching with brackets
by Enlil (Parson) on Jul 12, 2005 at 09:05 UTC
    The "easy" way is to use: Regexp::Common::balanced have a look at "perldoc -q nesting" as well for more information on this FAQ

    -enlil

Re: pattern matching with brackets
by gopalr (Priest) on Jul 12, 2005 at 09:17 UTC
    use strict; undef $/; my $text=<DATA>; my @array=(); @array=$text=~m#void xxx \(void\)(\{[^\{\}]+\})#gs; $"="\n"; print "@array"; __DATA__ void xxx (void){ #inside function 1 } void xxx (void){ #inside function 2 } void xxx (void){ #inside function 3 }
Re: pattern matching with brackets
by dirac (Beadle) on Jul 12, 2005 at 09:51 UTC
    This code works if you have functions without { } inside ..
    #!/usr/bin/perl -w use strict; my $code = <<'EOC'; #import <stdio.h> #import <time.h> char * point() { printf("[function point]\n"); return "point"; } char * mouse() { printf("[function mouse]\n"); return "mouse"; } EOC my @subs = ( $code =~ /\{([^\{\}]+)\}/g ); print "@subs\n";
Re: pattern matching with brackets
by Anonymous Monk on Jul 12, 2005 at 11:28 UTC
    I've found this, but can't get it to work :o(
    $np = qr{ \( (?: (?> [^()]+ ) # Non-parens without backtracking | (??{ $np }) # Group with matching parens )* \) }x;
    Can someone which more experience than me give me q uick example as to how to santch out what is *inside* some {} brackets, for example snatching out and array of the contents of multiple c functions in the same file. Thank you...