in reply to help needed with match multiple lines

You seem to be certain that your regex is not matching multiple lines but is otherwise correct.

And the thing is, your code works for multiple lines. When I substituted your regex with /(class).*(pattern)/si, and planted an appropriate file with 'class' and 'pattern' each on its own line, the regex matched and list.txt had a line in it.

So you don't have a problem with multiline matching, you have a problem with your regex not matching the text you want to match.

Replies are listed 'Best First'.
Re^2: help needed with match multiple lines
by Anonymous Monk on Jun 20, 2008 at 17:43 UTC
    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
      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