I think you should also handle the <![CDATA[...]]> sections.

$xml =~ s{(?:(<!\[CDATA\[.*?\]\]>)|(<[^\s?!>]+))}{ $1 or do {(my $tagname = $2 ) =~ tr/.//d;$tagname;} }seg; # or ... sligthly more efficient for XMLs with most tags without the d +ots $xml =~ s{(?:(<!\[CDATA\[.*?\]\]>)|(<[^\s?!>\.]+\.[^\s?!>]+))}{ $1 or do {(my $tagname = $2 ) =~ tr/.//d;$tagname;} }seg; # and now even more efficient thanks to moving the &lt; outside the or $xml =~ s{<(?:(!\[CDATA\[.*?\]\]>)|(<[^\s?!>\.]+\.[^\s?!>]+))}{ '<'. ($1 or do {(my $tagname = $2 ) =~ tr/.//d;$tagname;} ) }seg;
Benchmark:
my $XML = <<'__XML__'; <?xml version="1.0"?> <TOP> <SUB> <![CDATA[<SOME.OTHER.TYPE>BLAH</SOME.OTHER.TYPE> ]]> <THIS>STUFF</THIS> <SOME.TYPE>T</SOME.TYPE> <SOME.OTHER.TYPE>BLAH</SOME.OTHER.TYPE> <![CDATA[<SOME.OTHER.TYPE>BLAH</SOME.OTHER.TYPE> ]]> </SUB> </TOP> __XML__ use Benchmark; sub one { my $xml = $XML; $xml =~ s{(?:(<!\[CDATA\[.*?\]\]>)|(<[^\s?!>]+))}{ $1 or do {(my $ +tagname = $2 ) =~ tr/.//d;$tagname;} }seg; $xml =~ s{(?:(<!\[CDATA\[.*?\]\]>)|(<[^\s?!>]+))}{ $1 or do {(my $ +tagname = $2 ) =~ tr/.//d;$tagname;} }seg; $xml =~ s{(?:(<!\[CDATA\[.*?\]\]>)|(<[^\s?!>]+))}{ $1 or do {(my $ +tagname = $2 ) =~ tr/.//d;$tagname;} }seg; return; } sub two { my $xml = $XML; $xml =~ s{(?:(<!\[CDATA\[.*?\]\]>)|(<[^\s?!>\.]+\.[^\s?!>]+))}{ $1 + or do {(my $tagname = $2 ) =~ tr/.//d;$tagname;} }seg; $xml =~ s{(?:(<!\[CDATA\[.*?\]\]>)|(<[^\s?!>\.]+\.[^\s?!>]+))}{ $1 + or do {(my $tagname = $2 ) =~ tr/.//d;$tagname;} }seg; $xml =~ s{(?:(<!\[CDATA\[.*?\]\]>)|(<[^\s?!>\.]+\.[^\s?!>]+))}{ $1 + or do {(my $tagname = $2 ) =~ tr/.//d;$tagname;} }seg; return; } sub three { my $xml = $XML; $xml =~ s{<(?:(!\[CDATA\[.*?\]\]>)|(<[^\s?!>\.]+\.[^\s?!>]+))}{ '< +'. ($1 or do {(my $tagname = $2 ) =~ tr/.//d;$tagname;} ) }seg; $xml =~ s{<(?:(!\[CDATA\[.*?\]\]>)|(<[^\s?!>\.]+\.[^\s?!>]+))}{ '< +'. ($1 or do {(my $tagname = $2 ) =~ tr/.//d;$tagname;} ) }seg; $xml =~ s{<(?:(!\[CDATA\[.*?\]\]>)|(<[^\s?!>\.]+\.[^\s?!>]+))}{ '< +'. ($1 or do {(my $tagname = $2 ) =~ tr/.//d;$tagname;} ) }seg; return; } sub four { my $xml = $XML; $xml =~ s{<(?:(!\[CDATA\[.*?\]\]>)|(<[^\s?!>]+))}{ '<'. ($1 or do +{(my $tagname = $2 ) =~ tr/.//d;$tagname;} ) }seg; $xml =~ s{<(?:(!\[CDATA\[.*?\]\]>)|(<[^\s?!>]+))}{ '<'. ($1 or do +{(my $tagname = $2 ) =~ tr/.//d;$tagname;} ) }seg; $xml =~ s{<(?:(!\[CDATA\[.*?\]\]>)|(<[^\s?!>]+))}{ '<'. ($1 or do +{(my $tagname = $2 ) =~ tr/.//d;$tagname;} ) }seg; return; } timethese( 100000, { one => \&one, two => \&two, three => \&three, four => \&four, }); __END__ Benchmark: timing 100000 iterations of four, one, three, two... four: 2 wallclock secs ( 1.89 usr + 0.00 sys = 1.89 CPU) @ 52 +882.07/s (n=100000) one: 10 wallclock secs ( 9.53 usr + 0.00 sys = 9.53 CPU) @ 10 +492.08/s (n=100000) three: 2 wallclock secs ( 1.86 usr + 0.00 sys = 1.86 CPU) @ 53 +792.36/s (n=100000) two: 6 wallclock secs ( 6.27 usr + 0.00 sys = 6.27 CPU) @ 15 +959.14/s (n=100000)

Jenda
Enoch was right!
Enjoy the last years of Rome.


In reply to Re^2: Replace XML tag names. by Jenda
in thread Replace XML tag names. by kyle

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.