Hello anirudhkumar_r,
Update: parv’s solution is simpler, therefore better!
To prevent recursive replacement, you can use negative look-behind assertions as follows:
#! perl use strict; use warnings; my $s = '&lt; < &xyz &gt;abc 123>456 me&you'; $s =~ s/ (?<!&) < /</gx; $s =~ s/ (?<!&) > />/gx; $s =~ s/ &lt; /</gx; $s =~ s/ &gt; />/gx; $s =~ s/ & /&/gx; print $s;
Output:
22:03 >perl 1084_SoPW.pl < < &xyz >abc 123>456 me&you 22:03 >
See “Look-Around Assertions” in perlre#Extended-Patterns.
Hope that helps,
| Athanasius <°(((>< contra mundum | Iustus alius egestas vitae, eros Piratica, |
In reply to Re: Avoid recursive replacement using regex
by Athanasius
in thread Avoid recursive replacement using regex
by anirudhkumar_r
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |