Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

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

by tinita (Parson)
on May 21, 2007 at 14:53 UTC ( [id://616581]=note: print w/replies, xml ) Need Help??


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

i personally would be interested in why the following happens:
"\n" =~ /\n.*\z/; # matches "\n" =~ /.*\z/; # doesn't match. i would expect it to match "\n" =~ /[^\n]*\z/; # matches. like expected. but [\n]* is like .*
/s or not /s doesn't have to do something with this, or at least it shouldn't, i think.

Replies are listed 'Best First'.
Re^5: Strange regex to test for newlines: /.*\z/
by shmem (Chancellor) on May 21, 2007 at 15:44 UTC
    "\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}
      "\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...

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://616581]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (7)
As of 2024-04-19 09:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found