in reply to Re: The "anchor" misnomer in regexes
in thread The "anchor" misnomer in regexes

I’m not sure you have the semantics all correct: \z is always “end of string” and nothing else. \Z and $ are identical by default and mean “end of string, but before a newline if that’s the last character.” However, if you use the /m regex modifier, then $ (but not \Z) changes to line-based semantics and essentially says “before the next newline or at the end of the string (whichever is first).”

Just to straighten things for anyone for whom they were unclear.

(The clarification does, of course, not affect what you said about keeping state for $.)

Makeshifts last the longest.

Replies are listed 'Best First'.
Re^3: The "anchor" misnomer in regexes
by ysth (Canon) on Dec 26, 2005 at 06:13 UTC
    Additional clarification, just to muddle things a little:
    However, if you use the /m regex modifier, then $ (but not \Z) changes to line-based semantics and essentially says “before the next newline or at the end of the string (whichever is first).”
    The choice isn't next newline or end of string and isn't whichever is first; regular leftmost/longest backtracking rules apply. So this:
    ("\n" x 10) =~ /(\s+?$)\s{3}\z/m; print length $1;
    gives 7, with the $ matching before the third-to-last newline. Same results without the ?.

      What are you proving? The \s+ part will start as far on the left as it can, and since your string is all newlines and you’re always using \s when you ask for character matches and have turned on /m, all character matches can match anywhere and the $ can match between any characters. So you’re not actually saying anything. No part of your pattern is constrained in any way, given the data you’re running it against.

      I guess the bit I wrote about firstness does not actually say anything either, since “whichever is first” means “whichever is left-most” anyway and at any location between characters, only one of these conditions can be true. So $ in multiline mode matches before any newline or before the end of the string, and that’s that.

      Makeshifts last the longest.