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

Is any know why '\' at end of line in regex is ignored (perl 5.8.0)? Is this a bug or feature? ;-) It's unusual for perl to have no special meaning for cool thing like this... ;-)
# 2 lines regex, \n not escaped $re1 = qr/as sd/; # 2 lines regex, \n escaped $re2 = qr/as\ sd/; for ($re1, $re2) { print "$_\n"; print "ok1\n" if "as\nsd" =~ /$_/; print "ok2\n" if "as\\\nsd" =~ /$_/; print "ok3\n" if "assd" =~ /$_/; print "ok4\n" if "as\\sd" =~ /$_/; } __END__ (?-xism:as sd) ok1 (?-xism:as\ sd) ok1

Replies are listed 'Best First'.
Re: escaped \n in regex
by pfaut (Priest) on Feb 20, 2003 at 00:31 UTC

    Perl doesn't do things to be 'cool'. It does things to be useful. Besides, what 'cool' thing did you expect perl to do with that, anyway?

    You embedded a literal newline in your regular expression. That matches newlines in the string you match against as you can see by your test code. Putting a backslash in front of it does nothing since it's not a special character.

    --- print map { my ($m)=1<<hex($_)&11?' ':''; $m.=substr('AHJPacehklnorstu',hex($_),1) } split //,'2fde0abe76c36c914586c';
Re: escaped \n in regex
by steves (Curate) on Feb 20, 2003 at 00:34 UTC

    I think this is consistent. Perl lets you embed hard newlines in string literals, which are taken for what they are -- newlines. Double quoted interpolation rules also say that \ followed by anything that's not a recognized metacharacter sequence interpolates to the character following the backslash. This is why the two above are equivalent ... which is what I think the question is.

Re: escaped \n in regex
by Pardus (Pilgrim) on Feb 20, 2003 at 13:28 UTC
    Its a feature, since the line break is just an other non-special ascii char it's escaped version behaves exactly the same as any other non-special escaped char, i.e. it is just the same as the ascii char itself.
    --
    Jaap Karssenberg || Pardus (Larus)? <pardus@cpan.org>
    >>>> Zoidberg: So many memories, so many strange fluids gushing out of patients' bodies.... <<<<