(Note: if you have the ability to answer this instantly, please don't respond -- or at least respond in a table with the background color and the text color the same.)

You want to match "foo\nbar", using the /m modifier to a regex.

$string = "...foo\nbar..."; if ($string =~ /foo$bar/m) { # ... }
and
$string = "...foo\nbar..."; if ($string =~ /foo$^bar/m) { # ... }
How successful are you? Also consider
$string = "...foo\nbar..."; if ($string =~ / foo$ ^bar /mx) { # ... }
Now how successful are you? Please explain your answers, and give any insight you have into the cause of the problem.

japhy -- Perl and Regex Hacker

Replies are listed 'Best First'.
Re: (Regex Quiz) Multi-line Matching
by Albannach (Monsignor) on May 22, 2001 at 23:51 UTC
    Ok, here's my stab at it with some trial and error but without any research:

    My first reaction was "why use the anchors?" as you're specifying the text on both sides of the newline, so the anchors are redundant: if ($string =~ /foo\nbar/m) seems to work fine.

    As to why the anchors don't work, and apart from them being interpreted as parts of variables (no $bar in your first example) I think it is because you are not allowing for the actual newline character. The anchors are not characters, just locations.

    Now for a question of my own: Why does if ($string =~ /foo.bar/m) or similar not work?

    (BTW, I'm planning to re-do my living room with the colour selection that results from this thread, so select carefully please!)

    Update: Thanks nysus, I'm glad I said I hadn't done any research as that's practically the first line in the quick reference section on regex (blush). You can tell I never need to match a newline!

    --
    I'd like to be able to assign to an luser

      The "dot" character does not recognize "newline" characters unless the "s" modifier is used, like so:
      /foo.bar/ms

      $PM = "Perl Monk's";
      $MCF = "Most Clueless Friar";
      $nysus = $PM . $MCF;

(tye)Re: (Regex Quiz) Multi-line Matching
by tye (Sage) on May 22, 2001 at 22:12 UTC

    I wasn't able to answer this instantly.

    I did find that this helps:

    perl -debug DB<1> $string= "...foo\nbar..."; DB<2> x $string =~ /(foo$bar)/m; DB<3> x $string =~ /(foo$^bar)/m; DB<4> x $string =~ /( foo$ ^bar )/mx;
    note the added parens.

    You may also want to add use strict and turn on warnings (though those don't work very well when using the debugger like above).

    You may also be interested in seeing how backslashes change things.

            - tye (but my friends call me "Tye")
Re: (Regex Quiz) Multi-line Matching
by nysus (Parson) on May 22, 2001 at 23:38 UTC
    You need to use an octal (or hex) code for the line feed character for it to work. I used the octal, like so:
    if ($string =~ /foo\012bar/m)
    Had to consult the perlre.

    $PM = "Perl Monk's";
    $MCF = "Most Clueless Friar";
    $nysus = $PM . $MCF;

Re: (Regex Quiz) Multi-line Matching
by nysus (Parson) on May 22, 2001 at 23:20 UTC
    I'm not too clear on the rules. Are we required to use the /foo$bar/m and /foo$^bar/m REs?

    $PM = "Perl Monk's";
    $MCF = "Most Clueless Friar";
    $nysus = $PM . $MCF;

      It's your job to find out why those fail. Then, describe how you would tackle the problem. You might not use an anchor at all, but explain why the two attempts fail.

      japhy -- Perl and Regex Hacker