in reply to Re: help needed with match multiple lines
in thread help needed with match multiple lines

Hi,
Thanks much for your reply. Here is part of the text. I am trying to match the name and the number.
class="report" width="15%"><a href="rm=mode2&id=1">12R</a>< +/td> class="report" width="15%">567</td> class="report" width="15%"><a href="rm=mode2&id=1">14R</a>< +/td> class="report" width="15%">129</td>
When I run the codes, it seems to be in endless loop. Not sure what's the problem. Thanks!!!
Joice

Replies are listed 'Best First'.
Re^3: help needed with match multiple lines
by jethro (Monsignor) on Jun 20, 2008 at 19:10 UTC
    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