Live data from Hacker News

Why do regexes use `$` and `^` as line anchors? (2024)

buttondown.com

11–20 of 48 posts

Re: Why do regexes use `$` and `^` as line anchors? (2024)

#11
post #9
post #6

My AZERTY keyboard conveniently has them side by side (^ left of $). Could it be a similar reason?

AZERTY is primarily a French keyboard layout, to my knowledge. The standard QWERTY layout for the number key row is `/~, 1/!, 2/@, 3/#, 4/$, 5/%, 6/^, 7/&, 8/*, 9/(, 0/), -/_, =/+. I don't know how far back the mapping of shift keys for the numbers go, but I'd be shocked if there was any around the 1960s or 1970s that put them like your AZERTY keyboard.

Yes (I am French). Those characters are found on the top letter row: AZERTYUIOP^$

Re: Why do regexes use `$` and `^` as line anchors? (2024)

#13
> But also, $ is so important in business that every typewriter has one.

Some of the older Baudot code ones actually had a £ but not a $ symbol. After we'd decided that 5-key chorded keyboards were not the way forwards, and made QWERTY ones, we still had this encoding to deal with: https://hackaday.com/2015/09/27/demonstrating-baudot-code/

The US version then put the $ sign on what we'd today call ^D, which originally was ENQ (a code that still exists in ascii today, and was sort of the pre-TCP version of a SYN).

Re: Why do regexes use `$` and `^` as line anchors? (2024)

#14
I remember when learning regexes, struggling to remember which of ^ and $ were the beginning or end of the line. I know that my (and many other) shells used $ as the prompt symbol - i.e. at the beginning of what I was to type in the terminal. It being at the beginning of that line made it easy for me to remember that it also marks the beginning of regexes.

Of course I was 100% incorrect as $ is in fact the end-of-line marker so now I just remember that "it's the opposite"!

Re: Why do regexes use `$` and `^` as line anchors? (2024)

#18

I remember when learning regexes, struggling to remember which of ^ and $ were the beginning or end of the line. I know that my (and many other) shells used $ as the prompt symbol - i.e. at the beginning of what I was to type in the terminal. It being at the beginning of that line made it easy for me to remember that it also marks the beginning of regexes. Of course I was 100% incorrect as $ is in fact the end-of-lin…

> so now I just remember that "it's the opposite"

"No, the other left..."

For me it was easier to remember what ^ was, as it looks like a pointer of sorts, so felt natural it would be the beginning. Like how a string variable points to the first character.

Re: Why do regexes use `$` and `^` as line anchors? (2024)

#19

I remember when learning regexes, struggling to remember which of ^ and $ were the beginning or end of the line. I know that my (and many other) shells used $ as the prompt symbol - i.e. at the beginning of what I was to type in the terminal. It being at the beginning of that line made it easy for me to remember that it also marks the beginning of regexes. Of course I was 100% incorrect as $ is in fact the end-of-lin…

Idk why anyone would ever "learn regex's" without the use of a look up table. It's not like timestables where having it memorized will really accelerate other aspects of life. Knowing what ^ does by memory will almost never benefit you.

Re: Why do regexes use `$` and `^` as line anchors? (2024)

#20

I remember when learning regexes, struggling to remember which of ^ and $ were the beginning or end of the line. I know that my (and many other) shells used $ as the prompt symbol - i.e. at the beginning of what I was to type in the terminal. It being at the beginning of that line made it easy for me to remember that it also marks the beginning of regexes. Of course I was 100% incorrect as $ is in fact the end-of-lin…

Now that you mention it, I weirdly never struggled with this. I wonder if it's because I first started using the terminal on macOS, which I think by default starts prompts with % instead of $ or >.

Trying to consciously think about the gut-feeling that makes me remember this, all I can come up with is that $ feels "heavier" than ^, which just leads it to naturally feel like the end of a line. Perhaps its verticalness makes it feel like the vertical blinking cursor, and that's what this gut-feeling is really about? I'm not sure, the mind is a mystery sometimes haha.

Post reply on HN