Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why ^ and $ were choosen to represent beginning and end of line in regex? [closed]

Tags:

regex

Maybe there is some hidden and forgotten by everyone reason why those keys were choosen? In the same vein as this link http://www.catonmat.net/blog/why-vim-uses-hjkl-as-arrow-keys/ explains use of hjkl in vim and ~ in bash, maybe there is some story behind ^ and $? Actually, on the picture in that article, the terminal shown doesn't even have a ^ key.

I'd really appreciate if somebody will tell or show the keyboard layout used by Ken Thompson when he written a version of QED that supported regex. (And if people stop trying to be funny in the comments).

edit:

These are some amasing replies. Even if they are just guesses, they may serve like a great mnemonic material. Thank you! Just wish you didn't put it on hold so fast, maybe some people were too slow to answer.

like image 890
Peret Finctor Avatar asked Nov 09 '22 00:11

Peret Finctor


1 Answers

It's hardly possible to answer this question today because regexps were introduced more than fifty years ago. I may share versions that seem the most reasonable for me.

Dollar sign was symbolizing the end of line yet in ed editor. First practical implementation of RE were introduced namely in ed and derivatives (like QED). And dollar sign was reused. It meant either the end of line or the last line, depending on context. This is more or less clear but the question why dollar sign was initially denoted the end of something yet remains. It ('$') seems to be an "ASCII approximation" of a typographic "end of paragraph" sign.

As for '^' as the beginning of line, it's probably an "ASCII approximation" of Lambda Greek letter which stood for an empty expression in early theoretical works like this: http://www.fing.edu.uy/inco/cursos/intropln/material/p419-thompson.pdf

like image 171
AndreyS Scherbakov Avatar answered Nov 14 '22 23:11

AndreyS Scherbakov