use warnings;
use strict;
while (my $line = <DATA>) {
# Assume one "element" per line
if ($line =~ /<(\w+)>(.*?)<\/\1>/) {
(my $midstr = $2) =~ tr/<>/[]/;
$line = "<$1>$midstr</$1>\n";
}
print $line;
}
__DATA__
<sometag> my data here </sometag>
<anothertag> further text </anothertag>
<furthertag> not good as contains <this> in angle brackets </furtherta
+g>
<byetag> another possibility is <more> <than> one <angle pairs> in her
+e </byetag>
Prints:
<sometag> my data here </sometag>
<anothertag> further text </anothertag>
<furthertag> not good as contains [this] in angle brackets </furtherta
+g>
<byetag> another possibility is [more] [than] one [angle pairs] in her
+e </byetag>
DWIM is Perl's answer to Gödel
|