One thing wrong with your code is that you use '$' at the end of your regexp. This will only match the absolute end of your string, not a line ending. The regex switch m lets '$' also match line endings, i.e. use /.../msi

But that alone won't help you, because you have so many greedy matches in your regex (i.e. .*), that you will match horribly wrong in any nontrivial html file. Changing all .* to .*? will make a big difference.

BUT what happens when there is a line ...<td> SPACE RETURN. Your regex won't match it because you forgot a \s* before the '$'. Instead it will match a few more lines until the next td without spaces behind it

So you see, getting this right is not trivial. Better use a module like cfreak suggested

By the way, I didn't get any endless loop with your example data. You might check with a print statement in your while loop if that is looping, but it shouldn't. The while loop isn't really necessary since you read the file in one take, so you could substitute it with if (defined($_=<IN>)) which eliminates the while and provides the hidden magic of the while(<>) loop


In reply to Re^3: help needed with match multiple lines by jethro
in thread help needed with match multiple lines by Anonymous Monk

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.