in reply to Re^4: Strange regex to test for newlines: /.*\z/
in thread Strange regex to test for newlines: /.*\z/

"\n" =~ /\n.*\z/; # matches

Obvious, I think. You match a "\n", then EOS (end of string).

"\n" =~ /.*\z/; # doesn't match. i would expect it to match

perl -D512 tells anchored(MBOL) (i.e. multiline beginning of line, see perldebguts) with that one, which anchoring doesn't happen with

"\n" =~ /[^\n]*\z/; # matches. like expected. but [\n]* is like .*

but why?

"\n" =~ /.?\z/;

matches, as does

"\n" =~ /.{0,}\z/;

I can't get a mental model of why the previous one should, but the next one should not match:

"f\n" =~ /.?f\z/;

Weird. Rather inconsistent, if not buggy.

--shmem

_($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                              /\_¯/(q    /
----------------------------  \__(m.====·.(_("always off the crowd"))."·
");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}

Replies are listed 'Best First'.
Re^6: Strange regex to test for newlines: /.*\z/
by betterworld (Curate) on May 22, 2007 at 00:46 UTC
    "\n" =~ /\n.*\z/; # matches
    Obvious, I think.

    If this one is obvious, then the original one should be obvious too. Compare:

    "\n" =~ /\n.*\z/; # matches "\n" =~ /.*\z/; # should match but doesn't

    The pattern gets shorter, but doesn't match any more...