[Israel.pm] recursive regex

Shlomo Yona shlomo at cs.haifa.ac.il
Wed Jun 9 02:24:41 PDT 2004


On Wed, 9 Jun 2004 Yossi.Itzkovich at ecitele.com wrote:

> A,B,C  are macros that are used later, and I want to save them in my script
> for other needs.
> if I do:
> 
> while ( /BEGIN_(\w+).+?END_(\1)/sg)
> { print "$1,$2;}
> 
> 
> I don't see the C macro, because it is inside B.  How can I solve it nicely
> ?

Hello.

Is this what you have in mind?

while(<DATA>) {
        print_begin_end_macro($1) if /BEGIN_(\S+)/;
}

sub print_begin_end_macro {
        my ($what)=@_;
        while(<DATA>) {
                if (/END_$what/) {
                        print qq{BEGIN_$what\tEND_$what\n} if /END_$what/;
                        return;
                }
                print_begin_end_macro($1) if /BEGIN_(\S+)/;
        }

}

__DATA__
BEGIN_A
...
END_A

BEGIN_B
BEGIN_C
...
END_C
END_B


-- 
Shlomo Yona
shlomo at cs.haifa.ac.il
http://cs.haifa.ac.il/~shlomo/



More information about the Perl mailing list