Live data from Hacker News

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

buttondown.com

21–30 of 48 posts

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

#21

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…

But isn't the $ usually at the end of the prompt? :)

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

#22

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…

I never found this hard, because $ is conventionally used at the end of many strings in Windows land (e.g. the names of network drives), which probably dates back to the CP/M dollar-terminated string.

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

#23

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.

It does come in handy if you search for stuff with regular expressions a lot!

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

#24

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…

For me, it's '^' pointing up, up being before, or first if time flows from top to bottom.

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

#25
post #7

> $ is so important in business that every typewriter has one I actually had a typewriter without one. I would simulate it with S / which was not very satisfactory but generally understandable in context.

Was it a US typewriter? This peso symbol is very important in business in the US and Latin America but not, for example, England, Ireland, India, Finland, or Sweden.

I know this symbol is used for pesos, but when 24 countries refer to it as "dollar"[0] compared to 7 for "peso" it seems fair to call it a dollar symbol? Not to mention that it's referred to as "dollar" in unicode.

It's also very interesting to me that [1] mentions it can have one or two bars but in the list above the double-barred version is not only not in unicode but refers specifically to the cifrão[2].

I guess the TLDR is currency stuff is confusing and nonstandard more often than not.

[0] https://en.wikipedia.org/wiki/Currency_symbol#List_of_curren...

[1] https://en.wikipedia.org/wiki/Dollar_sign

[2] https://en.wikipedia.org/wiki/Cape_Verdean_escudo

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

#27
post #23

Earlier quoted context omitted.

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.

It does come in handy if you search for stuff with regular expressions a lot!

I do search with regex pretty often and as a result have memorized these, but if I was mentoring someone I would never encourage them to memorize it.

Start with . and .*, then add additional stuff as you get comfortable

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

#28
post #25
post #7

Earlier quoted context omitted.

Was it a US typewriter? This peso symbol is very important in business in the US and Latin America but not, for example, England, Ireland, India, Finland, or Sweden.

I know this symbol is used for pesos, but when 24 countries refer to it as "dollar"[0] compared to 7 for "peso" it seems fair to call it a dollar symbol? Not to mention that it's referred to as "dollar" in unicode. It's also very interesting to me that [1] mentions it can have one or two bars but in the list above the double-barred version is not only not in unicode but refers specifically to the cifrão[2]. I guess t…

The double-barred version is not in Unicode because it's just font variations for the same abstract character. Just like the letter g can be written with one or two loops (just look at Helvetica vs Times); Unicode treats it as a font difference. This is called an allograph. Using Baskerville in iOS to render the $ character shows two bars (there are many different variations of Baskerville though).

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

#30
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.

During the relevant time there were two predominant layouts for the number key row: “typewriter-paired”, which is more or less same as the one used today and “bit-paired” where the symbols had the same order as they have in ASCII (ie. pressing a number key while holding shift produces a character represented by that number's ascii codepoint minus 0x10)
Post reply on HN