in reply to Re^2: Substitution for braces
in thread Substitution for braces

Is this a different question to your original question? I don't understand it. The data in your original question does not have <cc> in it.

Replies are listed 'Best First'.
Re^4: Substitution for braces
by gem555 (Acolyte) on Jul 09, 2009 at 08:43 UTC
    In xml file, & I have to substitute in <cc>Some text </cc> So when i read the file, the <cc> tag is with other tags also. Even there also & is replaced with '-'. In the example below:
    if($line =~ /<cc>([\s\S]+?)<\/cc>/){ #$line = $1; $line =~ s/&amp;amp;/-/g; $line =~ s/&amp;/-/g; $line =~ s/-$//g; }
    if I assign $line to $1, then in the output rest of the strings are removed and only $1 is visible.

      This seems to be a new problem, only partly related to your original post. I'm sorry, but I don't understand what your current problem is. Please provide a self-contained program and the input and output data, and what you expect. Please also put all your &, &amp; and &amp;amp; in between <code>...</code> tags, so we can understand when you mean & and when you mean &amp;.

      Judging from your description, my imagination is that you want to replace &amp; with - except within <cc>...</cc> tags. If that's true, then regular expressions are the wrong tool for the general case, and you should use an XML parser like XML::LibXML or XML::Twig.

        #!/usr/bin/perl use strict; use warnings; my $filename = 'library.xml'; use XML::LibXML; my $parser = XML::LibXML->new(); my $doc = $parser->parse_file($filename); #my $book; foreach my $title ($doc->findnodes('/library/book/title')) { my $name= $title->to_literal ; print $name; }
        Can I print just the title without giving "'/library/book/title'". There are many XML files and each have a different structure. How can I restrict to find the only the <cc> tag and get the value inside it.