A better solution might be  ((?:<[^>]*>)+)

(?:<[^>]*>)+ or even just plain  (?:<[^>]*>) allows a pair of tags with no intervening blanks to match or to be excluded from matching, respectively, thereby potentially destroying open-tag/close-tag synchronization: in either case, the "pair" of tags between which blanks are eliminated is incorrect. This can be remedied by changing the quantifier on  $blank, but, as another reply has suggested, a proper HTML parser is really the best approach.

>perl -wMstrict -le "my $s = '<foo a=b></foo> <bar c=d> </bar> '; my $t; ($t = $s) =~ s@((?:<[^>]*>)+) +((?:<[^>]*>)+)@$1$2@g; print qq{'$t'}; my $tag = qr{ < [^>]* > }xms; my $blank = qr{ [ ] }xms; ($t = $s) =~ s{ ($tag) $blank+ ($tag) }{$1$2}xmsg; print qq{'$t'}; ($t = $s) =~ s{ ($tag) $blank* ($tag) }{$1$2}xmsg; print qq{'$t'}; print qq{'$s'}; " '<foo a=b></foo><bar c=d> </bar> ' '<foo a=b></foo><bar c=d> </bar> ' '<foo a=b></foo> <bar c=d></bar> ' '<foo a=b></foo> <bar c=d> </bar> '

Update: Added another example using  * quantifier.


In reply to Re^2: spaces removed in backreference by AnomalousMonk
in thread spaces removed in backreference by Allasso

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.