[Israel.pm] recursive regex

Shlomo Yona shlomo at cs.haifa.ac.il
Wed Jun 9 03:40:31 PDT 2004

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

> Shlomo,

Hello, Yossi.

> It works great !

I'm glad. I wasn't sure that was what you wanted.

> But if I understand it correctly  it scans the data M+1 times (M=macro).

It scans your data one per line. This is something you
cannot reduce, as you have to see every line at least once,

> It is not a problem in my case, just of curiosity.

The only thing this solution is problematic about is that
for hight nesting levels it tends to pile up $what scalars
on the stack. Still -- unless you're facing with nesting
levels deeper than several millions, I wouldn't worry :-)

> 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+)/;
>         }
> }

Shlomo Yona
shlomo at cs.haifa.ac.il

More information about the Perl mailing list