This kind of problem is exactly why parsing HTML with regexes is not recommended. Sooner or later one runs into this sort of problem: you need to treat something one way if it outside certain tags and another way inside those tags.

I wonder if you could explain more about what you are trying to accomplish. Where is this white space you are replacing - are you trying to beautify the layout of an HTML file? Or are you trying to do replacements inside some tags but not others (like <pre>)?

If you are beautifying, I would seriously consider using an HTML parser like HTML::Parser or some similar module (search CPAN - there are a lot of variant. This will hand the document to you node by node and you pretty print each element exactly as you wish with as much or as little white space as you desire. There might also be a CPAN module that does all this for you, although my very brief attempts at searching (on the key words "html", "beautify") turned up nothing useful. Your mileage may vary.

If you are doing your magic on attribute values or the text between tags, I still strongly recommend that you consider using an HTML parsing module. Instead of trying to guess at whether you are operating on the right sort of tag, a parser will hand you the HTML element by element and you can choose exactly which elements you wish to beautify and what part of those elements (attribute values, text between tags).


In reply to Re: regex in form !regex->regex<-!regex by ELISHEVA
in thread regex in form !regex->regex<-!regex by forestcreature

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.