[Israel.pm] recursive regex

Omer Zak omerz at actcom.co.il
Wed Jun 9 02:43:57 PDT 2004


Another solution is to transform your data into XML (should not be 
difficult, given the pattern matching and replacing capabilities of Perl).

This involves also transforming the tokens, which should recursively 
match, into XML tags.

Then use one of the standard XML parsers to manipulate your data.

                                              --- Omer
There is no IGLU Cabal.  <IGLU><Cabal></IGLU></Cabal> is not legal XML.
My blog is at http://www.livejournal.com/users/tddpirate/

My opinions, as expressed in this E-mail message, are mine alone.
They do not represent the official policy of any organization with which
I may be affiliated in any way.
WARNING TO SPAMMERS:  at http://www.zak.co.il/spamwarning.html

Shlomo Yona wrote:
> 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




More information about the Perl mailing list