in reply to How to Extract Nested IF/THEN elements
However it would be so much easier to do it with Text::Balanced or Parse::RecDescent that its unlikely anyone would try. Here is a solution using Text::Balanced
use Text::Balanced 'extract_tagged'; use Data::Dumper; sub get_ifs { my $str=shift; my $list=shift || []; my ($extracted, $remainder, $prefix, $open, $inside, $close)=extract_tagged($str,"IF","ENDIF","(?s).*?(?=IF)"); if ($extracted) { push @$list,$extracted; get_ifs($inside,$list); get_ifs($remainder,$list) if $remainder; } return $list; } print Dumper(get_ifs(<<ENDOFIFS)); IF(A) anytext IF(B) IF(C) anytext ENDIF IF(D) anytext ENDIF ENDIF ENDIF ENDOFIFS
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
(Clarification of) How to Extract Nested IF/THEN elements
by demerphq (Chancellor) on Jan 24, 2002 at 22:01 UTC | |
|
Re: Re: How to Extract Nested IF/THEN elements
by Al Shiferaw (Initiate) on Jan 25, 2002 at 02:54 UTC |