| Refresh | Home EGTry.com

match unnested xml elements


perl script

use strict;

 my $text=<<'_end';
<prompt>
 <content>
   <content>
    walk this
   </content>
   other thing
   <content>
   two months
   </content>
  </content>
</prompt>
_end


print "before:\n $text\n";

$text =~ s/<content>.*?<\/content>/&hello($&)/sge;


print "after:\n $text\n";


sub hello {
 my $match=shift;
 my $pre="";
 if ($match=~ /(.*)<content>/s) {
   $pre=$1;
 }
 my $post=substr($match, length($pre));
 return $pre . "#REMOVED#";
}




output

before:
 <prompt>
 <content>
   <content>
    walk this
   </content>
   other thing
   <content>
   two months
   </content>
  </content>
</prompt>

after:
 <prompt>
 <content>
   #REMOVED#
   other thing
   #REMOVED#
  </content>
</prompt>