Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

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

by Joost (Canon)
on May 21, 2007 at 12:28 UTC ( [id://616539]=note: print w/replies, xml ) Need Help??


in reply to Strange regex to test for newlines: /.*\z/

It works because . does not match newlines by default. see the /s modifier at perlre.

  • Comment on Re: Strange regex to test for newlines: /.*\z/

Replies are listed 'Best First'.
Re^2: Strange regex to test for newlines: /.*\z/
by tinita (Parson) on May 21, 2007 at 12:33 UTC
    It works because . does not match newlines by default.
    i think betterworld knows this =)
    the point is, should .* match the empty string between foo\n and the end?

    update:

    "\n" =~ /\n.*\z/; # matches "\n" =~ /.*\z/; # doesn't match. huh? "\n" =~ /[^\n]*\z/; # matches. ??
Re^2: Strange regex to test for newlines: /.*\z/
by betterworld (Curate) on May 21, 2007 at 12:33 UTC
    It works because . does not match newlines by default.

    That's what I meant by "horizontal characters". But as "." has an asterisk after it, the regex should also match if "." does not match, shouldn't it?

      It would work if you used $ instead. \z specifically asks for the end of the string and . won't match the \ns without a /s on the end. So .* and \z aren't adjacent in your first example — without the /s.

      It probably would work with "\n" because .* could match 0 chars. But in your example, it already matched several chars before the \n which it refuses to slurp, so it (meaning the DFA) can't get to the \z at the end.

      Of course, the more I think about it... you might argue that the .* should backtrack, skip over the \n and continue to match the 0 chars right before the \z. I believe \z has a special meaning though. It's not really matching characters. It's more of a border on things, like \b, so I suspect there really aren't 0 characters before it because it's not really there.

      UPDATE: I'm completely convinced this is a bug based on "id-616551" below.

      -Paul

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (5)
As of 2024-04-18 03:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found