SPUG: When is a caret just a caret? And what about dollar?

Michael R. Wolf MichaelRWolf at att.net
Fri Aug 21 23:47:38 PDT 2009

I seem to remember that the meaning of caret in a regex is context- 
sensitive, and it can be an anchor, a complement, or just (with  
apologies to Sigmond) a caret.

My memory is that caret is an anchor iff it's the first character in a  
regex and a complement iff it's the first character in a character  
class, else it's a self-match.  Ergo, /^^[^^]^/ would match "beginning  
of line then caret then anything-but-a-caret then caret".  I can't  
seem to find support for this (long-held) belief.  Have I been wrong  
for this long?

And while I'm at it, how 'bout dollar?  I thought it was an anchor iff  
it was the last character in the regex, else it introduced a scalar  
variable for interpolation.

It seems that the documentation says that caret and dollar are  
*always* metacharacters.  Any ideas how I could have been mislead by a  
(seemingly) similar set of rules that I may have misinterpreted?  Has  
their meaning changed in previous versions of Perl (or the Perl regex  

Michael R. Wolf
     All mammals learn by playing!
         MichaelRWolf at att.net

More information about the spug-list mailing list