23

To clarify, this isn't a question about what a caret (^) or a dollar sign ($) does in a regular expression.

In modern keyboards with QWERTY layout, ^ comes after $ (i.e., ^ appears above the number 6 and $ above 4), so I always have a moment of hand-eye discoordination when I use them like ^foobar$.

So I was curious why, historically, the caret and dollar sign were chosen to match the beginning/end of a line because it would be more intuitive if they were reversed.

Perhaps they had a different keyboard layout back then? Apparently, the reason ESC was chosen to change mode in vi was that it made sense for Bill Joy's keyboard: why vim uses hjkl keys as arrow keys.

sshh
  • 346
  • 2
    I have no idea (not into history) but it might just as well date back to vi / Bill Joy and the very same keyboard, since the ^ is already located on the home key, as is ~... – frostschutz Jul 12 '20 at 11:58
  • 3
    The first regex implementation was Ritchie’s in QED, and that already used ^/$. Even before regexes, $ meant “end of buffer” in QED, perhaps that influenced the choice of the “end of line” character. I don’t know about ^. The keyboard referenced in the QED manual is the Teletype 37’s. – Stephen Kitt Jul 12 '20 at 12:20
  • I don't know, but it's possible the location of the symbol on the keyboard had nothing to do with the choice. – Andy Dalton Jul 12 '20 at 15:05
  • $ could be an ASCII replacement for Σ which often used as terminal character for describing formal languages. + and * are Kleen operators for sure. – andreoss Jul 13 '20 at 01:23
  • @StephenKitt, AFAIK, QED is by Ken Thompson, not Denis Ritchie. – Stéphane Chazelas Jul 13 '20 at 14:19
  • @Stéphane you’re right, it was Ken Thompson who added regex support to QED. – Stephen Kitt Jul 13 '20 at 14:56
  • Regular expressions came from mathematics and automata, it might be helpful to see if they had similar notation. – Barmar Jul 15 '20 at 18:34

1 Answers1

-6

historically? why?

I would rather say:

^ looks like an up arrow, that can be considered as "before", like in the text you read before is above the text you are reading now.

$ is used to expand variables, but if there is nothing after it, there is nothing to be expanded, so it being in the very end, with nothing after, have a completely new meaning, what is just perfect.

I think it is more a matter of being intuitive from the very beginning, what could be considered historical, I just have no proof to confirm it that way ;)

Also, you can always remap your keyboard and glue some paper over those symbols with the ones you like ;)