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

Can I use the code similar to this?
#!/usr/bin/perl while ($line = <DATA>){ $line =~ s/<cc>([\s\S]+?)<\/cc>/defined ($1) ? "(&amp;amp;|&amp;)" : ' +-'/eg; print $line; }
This prints out (&amp;|&). But replacing of &amp; and & doesnot gets replaced with '-'.

Replies are listed 'Best First'.
Re^3: Substitution for braces
by Corion (Patriarch) on Jul 09, 2009 at 06:59 UTC

    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.

      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.