Live data from Hacker News

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

buttondown.com

31–40 of 48 posts

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

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

It would depend on the age of the typewriter, even the US. Many early ones did not have dedicated keys for symbols, which were made as the parent describes: by going back and typing another character over them (e.g., S backspace / to get a dollar sign).

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

#32
post #23

Earlier quoted context omitted.

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

Quite. I use regex a lot, but mostly in bursts. Sometimes I still have to check how a negative lookbehind goes! The important thing, as with everything, is to try to keep in mind what is and isn't possible, even if you don't keep the specific syntactic details in your mental L1 cache.

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

#33

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…

“Carrots (^) cost dollars ($)” is a mnemonic for the order — the carrot comes first and the dollar sign comes last.

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

#34

At least for ^ I was always told that the ADM3a terminal keyboard had a HOME key with ~ and ^ on it, which is why ~ means "home directory", and ^ means "home of the line".

Correct on the ADM3a keyboard. In fact the OP site has a pic:

https://www.hillelwayne.com/post/always-more-history/

Whether that explains the use of tilde and caret, I dont know.

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

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

It was, a Smith-Corona, but it was a scientific version where the characters that were normally on the "shifted" top row number keys were instead subscripted numbers. So you could type chemical formulas with proper subscripts (or roll the paper down one notch and have superscripts) but it wasn't great for normal writing.

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

#36

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…

In many dialects of BASIC (my first programming language), $ comes at the end of a variable name (e.g., NAME$) to indicate that the variable was a string (that example would often be read out loud as name-string). Remembering that, I never had to do the "it's the opposite" thing (for this case... I've done it for many other things). It's one of the few things I still remember about BASIC because I don't think I've written any ~40 years.

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

#37
I seem to remember an early assembler style where if you wanted to define a string, a "$" was used as the terminator. Print routines would output chars until the "$". Also an early linker format that had symbols that always ended with a $ (cannot remember which system now, perhaps CP/M)

There is also Pascal's ^pointer syntax for the other end of things.

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

#38
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…

It's a peso sign. The English word for the Spanish "peso" at the time was "dollar", and the US dollar was initially defined to be equal to the Spanish "dollar". The US dollar, which was a coin, had the same value as the Spanish peso because it consisted of the same weight (in Spanish, "peso") of silver. So the US used the peso sign for its dollars. Unicode calls it a "dollar sign" because that's what ASCII calls it, and that's because ASCII is from the US. But it's really a peso sign.

https://en.wikipedia.org/wiki/Peso

> In most countries of the Americas, the symbol commonly known as dollar sign, "$", was originally used as an abbreviation of "pesos" and later adopted by the dollar. The dollar itself actually originated from the peso or Spanish dollar in the late 18th century.

https://en.wikipedia.org/wiki/Dollar_sign#History

> The symbol appears in business correspondence in the 1770s from Spanish America, the early independent U.S., British America and Britain, referring to the Spanish American peso,[1][2] also known as "Spanish dollar" or "piece of eight" in British America. Those coins provided the model for the currency that the United States adopted in 1792, and for the larger coins of the new Spanish American republics, such as the Mexican peso, Argentine peso, Peruvian real, and Bolivian sol coins.

https://en.wikipedia.org/wiki/Spanish_dollar

> Most theories trace the origin of the "$" symbol, which originally had two vertical bars, to the pillars of Hercules wrapped in ribbons that appear on the reverse side of the Spanish dollar.[6] ¶ The term peso was used in Spanish to refer to this denomination, (...)

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

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

It would depend on the age of the typewriter, even the US. Many early ones did not have dedicated keys for symbols, which were made as the parent describes: by going back and typing another character over them (e.g., S backspace / to get a dollar sign).

Sure, I used one back in the 01980s where "!" was "'." with a backspace in between to overstrike them. And of course that's also what _, ^, `, etc., are for.

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

#40
post #38
post #25

Earlier quoted context omitted.

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…

It's a peso sign. The English word for the Spanish "peso" at the time was "dollar", and the US dollar was initially defined to be equal to the Spanish "dollar". The US dollar, which was a coin, had the same value as the Spanish peso because it consisted of the same weight (in Spanish, "peso") of silver. So the US used the peso sign for its dollars. Unicode calls it a "dollar sign" because that's what ASCII calls it,…

> > also known as ... "piece of eight"

I wonder if it is a coincidence that "peso" sounds pretty similar to "piece of"

Post reply on HN