[HN Gopher] Why do regexes use `$` and `^` as line anchors? (2024)
___________________________________________________________________
Why do regexes use `$` and `^` as line anchors? (2024)
Author : srijan4
Score : 69 points
Date : 2025-01-21 18:12 UTC (4 hours ago)
(HTM) web link (buttondown.com)
(TXT) w3m dump (buttondown.com)
| SoftTalker wrote:
| > $ 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 wrote:
| 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.
| pc86 wrote:
| 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 cifrao[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_cur
| ren...
|
| [1] https://en.wikipedia.org/wiki/Dollar_sign
|
| [2] https://en.wikipedia.org/wiki/Cape_Verdean_escudo
| kccqzy wrote:
| 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).
| garciansmith wrote:
| 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).
| SoftTalker wrote:
| 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.
| er4hn wrote:
| > 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 wrote:
| My AZERTY keyboard conveniently has them side by side (^ left of
| $).
|
| Could it be a similar reason?
| amelius wrote:
| Exactly what I thought of, but on querty it is the other way
| around.
| jcranmer wrote:
| 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.
| franky47 wrote:
| Yes (I am French). Those characters are found on the top
| letter row: AZERTYUIOP^$
| dfox wrote:
| 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)
| hwayne wrote:
| I think this is unlikely, as the three people involved
| (Deustch, Lampson, and Thompson) are all American.
| tarkin2 wrote:
| Everything starts with someone pointing at something. Everything
| ends with dollars.
|
| That's how I remembered them anyhow
| tetris11 wrote:
| SHOW me the MONEY
| red_admiral wrote:
| > But also, $ is so important in business that every typewriter
| has one.
|
| Some of the older Baudot code ones actually had a PS 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).
| milliams wrote:
| 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"!
| magicalhippo wrote:
| > 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 wrote:
| 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.
| tom_ wrote:
| It _does_ come in handy if you search for stuff with regular
| expressions a lot!
| NotAnOtter wrote:
| 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
| grues-dinner wrote:
| 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.
| aylmao wrote:
| 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.
| binaryturtle wrote:
| But isn't the $ usually at the end of the prompt? :)
| wizzwizz4 wrote:
| 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.
| jiehong wrote:
| For me, it's '^' pointing up, up being before, or first if time
| flows from top to bottom.
| new_user_final wrote:
| "Carrots (^) cost dollars ($)" is a mnemonic for the order --
| the carrot comes first and the dollar sign comes last.
| teddyh wrote:
| IIRC, the original ASCII standard had | and - but later changed
| them to ^ and _, respectively.
| NotAnOtter wrote:
| So 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 wrote:
| 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".
| kjellsbells wrote:
| 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.
| ramesh31 wrote:
| As 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".
___________________________________________________________________
(page generated 2025-01-21 23:03 UTC)