in reply to The "anchor" misnomer in regexes

We often refer to \A \b \B \G \z \Z ^ $ as "anchors", but I feel this is a misnomer, a simplification of their real name (which would be something like "string location assertion")

The term used in perlretut is "zero width assertion". Unfortuntely, that document also is chock full of the "anchor" term, going so far as to say that anchors are an example of a zero-width assertions.

As to your example, I'm not sure that telling the person that's it's not anchored really helps, as that's not the real misunderstanding they're having. They could just as easily get it wrong looking for a character, not an "anchor":

$string = "this #is an #example string!"; $re = qr/#(.*?)!/;

They need to understand that their regex is just doing exactly what they told it to do -- match a "#" and then match anything (except a newline) until the end of line is matched. What they said they wanted and what they told the computer are two different things. (What's that MJD quote about "does what you say, not what you mean"?)

Telling them what they should do instead ("[^#]") isn't really helpful to them in the long run unless it's accompanied by the explanation of what the regex means to the computer and why that's different than what they think it means.

-xdg

Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.