slugger415 has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks, still trying to learn XML::Twig, in this case string substitution.

If I have a block of XML, e.g.:

<p>String AAA <b>varname</b> and string AAA and BBB</p>

How do I go about replacing string AAA without clobbering the b element? I know I can do this (while evaluating the p element):

my $sub = $elt->text(); $sub =~ s/AAA/ZZZ/g; $elt->set_text($sub);

But that will remove the inner element:

String ZZZ varname and string ZZZ and BBB

I've looked at getting children and descendants but nothing seems to give me just the text elements that I can manipulate separately.

Sorry if its a naive question, and thanks for the advice. Scott

Replies are listed 'Best First'.
Re: String substituion with XML::Twig
by poj (Abbot) on Jul 26, 2014 at 05:48 UTC
    Try $elt->subs_text( qr'AAA','ZZZ' );
    poj

      wonderful, thank you!