someone202 has asked for the wisdom of the Perl Monks concerning the following question:

Hi all, I am trying to perform a really easy task and just can't understand why it's doesn't work. I got the following text file on my solaris 10 machine:
NO_LOGFILE_MSG CLOSE_AFTER_READ HELPTEXT "This might indicate that a server is not assigned wi +th enough memory to operate. Please notify the production team." HELP "c0526ac8-c34f-71dc-01e8-c64177d90000" SEVERITY Minor APPLICATION "ApplicationServers"
I just trying to open it and get the the text after 'HELPTEXT' string, the one what overlaps two lines. I tried anything: s/\r\n|\n|\r//;, chomp. Please help me! Why it doesn't work ?

Replies are listed 'Best First'.
Re: I am gating MAD!
by Corion (Patriarch) on Feb 28, 2008 at 20:51 UTC

    It does not work because you are reading your file line by line, but you try to match over two lines.

    The error is on line 42.

    This post was presented to you by cooperation of Acme::ESP, the Prescient Perl Prophets and How (not) to ask a question.

Re: I am gating MAD!
by igelkott (Priest) on Feb 28, 2008 at 21:11 UTC
    Probably failing because you're trying to match across multiple lines. Could try to read the file into a single string and have your regex use the m or s modifier.

    For example:

    open(I,'<','error.log'); undef $/; $_ = <I>; print "$1\n" if /\s*HELPTEXT\s+(.*)\s*HELP/s;

    Of course, you shouldn't use (.*) in matches but if you're really impatient and the input stays this simple (doesn't report multiple errors), this should work.

      Thx, Forgot about the //s flag... Last thing: why this one doesn't work:
      /HELPTEXT\s+"(.+)"\s+/s
      Thx.
        "doesn't work" here means it gets more than you want, I'm guessing?

        The .+ is greedy; it matches as much as possible of your string so long as it is followed by " and whitespace. Use .+? to be non-greedy, or use [^"]+ to match only non-" characters.