28 ms·
Why do regexes use `$` and `^` as line anchors? (2024)
- amelius 2y agoIf invented now, with current Unicode definitions, what symbols would they use?
- deleted 2y ago[deleted]
- deleted 2y ago[deleted]
- SoftTalker 2y ago> $ is so important in business that every typewriter has one I actually had a typewriter without one. I would simulate it with S <backspace> / which was not very satisfactory but generally understandable in context.
- kragen 2y agoWas 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.
- pc86 2y agoI 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_currency_symbols_currently_in_use https://en.wikipedia.org/wiki/Currency_symbol#List_of_curren... [1] https://en.wikipedia.org/wiki/Dollar_sign https://en.wikipedia.org/wiki/Dollar_sign [2] https://en.wikipedia.org/wiki/Cape_Verdean_escudo https://en.wikipedia.org/wiki/Cape_Verdean_escudo
- kccqzy 2y agoThe 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).
- kragen 2y agoIt'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 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 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 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, (...)
- pc86 2y ago> > also known as ... "piece of eight" I wonder if it is a coincidence that "peso" sounds pretty similar to "piece of"
- garciansmith 2y agoIt 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).
- kragen 2y agoSure, 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.
- SoftTalker 2y agoIt 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.
- er4hn 2y ago> Thompson and Lampson are still alive and we can just ask them. I just sent emails to both, I'll update this if I get an answer! This is one of the great things about this age. The barrier of reaching out to some person and asking them a question has never been lower. I've done this for RFCs, I've done this for questions about blog posts I've discovered here on HN. I've gotten lots of responses from people and it's always been illuminating to me.
- franky47 2y agoMy AZERTY keyboard conveniently has them side by side (^ left of $). Could it be a similar reason?
- amelius 2y agoExactly what I thought of, but on querty it is the other way around.
- jcranmer 2y agoAZERTY 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.
- franky47 2y agoYes (I am French). Those characters are found on the top letter row: AZERTYUIOP^$
- dfox 2y agoDuring 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)
- hwayne 2y agoI think this is unlikely, as the three people involved (Deustch, Lampson, and Thompson) are all American.
- tarkin2 2y agoEverything starts with someone pointing at something. Everything ends with dollars. That's how I remembered them anyhow
- tetris11 2y agoSHOW me the MONEY
- red_admiral 2y ago> 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/ 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).
- milliams 2y agoI 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"!
- magicalhippo 2y ago> 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.
- NotAnOtter 2y agoIdk 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.
- tom_ 2y agoIt does come in handy if you search for stuff with regular expressions a lot!
- NotAnOtter 2y agoI 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
- grues-dinner 2y agoQuite. 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.
- teddyh 2y agoIIRC, the original ASCII standard had ↑ and ← but later changed them to ^ and _, respectively.
- fanf2 2y agoASCII 1963 was the first version of the standard and it had ← https://dl.acm.org/doi/10.1145/366707.367524 https://dl.acm.org/doi/10.1145/366707.367524 The 1965 draft had _ instead https://dl.acm.org/doi/10.1145/363831.363839 https://dl.acm.org/doi/10.1145/363831.363839 The first standard edition with _ was 1968 https://www.rfc-editor.org/info/rfc20 https://www.rfc-editor.org/info/rfc20 The 1977 version is also available https://nvlpubs.nist.gov/nistpubs/Legacy/FIPS/fipspub1-2-1977.pdf https://nvlpubs.nist.gov/nistpubs/Legacy/FIPS/fipspub1-2-197... Xerox PARC used ASCII 1963 (for reasons I have yet to unearth) so its programming languages Mesa and SmallTalk used ← for assignment and camelCase to separate words in identifiers. This stylistic quirk was carried over to later object-oriented programming languages.
- NotAnOtter 2y agoSo basically, they used them because they needed to assign them to something ang they knew type writers would have those characters due to their use in business & math. I mean. Fair enough. I guess.
- acegopher 2y agoAt 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".
- kjellsbells 2y agoCorrect on the ADM3a keyboard. In fact the OP site has a pic: https://www.hillelwayne.com/post/always-more-history/ https://www.hillelwayne.com/post/always-more-history/ Whether that explains the use of tilde and caret, I dont know.
- fanf2 2y ago^ for start of line predates the ADM3a
- ramesh31 2y agoAs with so many things in computer science, the answer comes down to "someone working on a UNIX system in the 70's had to make an arbitrary decision on something, which no one else then ever had a good reason to change".
- VogonPoetry 2y agoI 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.