in reply to The "anchor" misnomer in regexes
But the regex /\s+$/ is not anchored (and this grieves me). I've used this example time and time again when explaining regex reversal and that whole tangent, and it's useful again. If the regex were really anchored (that is, immovable), it would find the end of the string, and then match the regex in that context -- that is, the regex would have to terminate at the end of the string. As it stands, Perl can't optimize the regex that way, and we end up matching EVERY chunk of whitespace, and then testing to see if END-OF-STRING comes after it.
I think its important to remember this point only applies to \z (and maybe, I think, \Z) and where $ is the same as \z.
The logic makes sense when $ is actually end of line and not end of string, as it means doing a scan through the string to find the newline, which means it might as well keep track of state as it goes so it knows where the spaces start.
I guess what is a pity is that there isn't an optimization that handles this as a special case. It seems to me that patterns of this type could be special cased somehow. (And I mean this in the general case of /CLASS QUANTIFIER EOS/ and not just /\s+$/)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: The "anchor" misnomer in regexes
by Aristotle (Chancellor) on Dec 26, 2005 at 05:59 UTC | |
by ysth (Canon) on Dec 26, 2005 at 06:13 UTC | |
by Aristotle (Chancellor) on Dec 26, 2005 at 06:30 UTC |