Hacker News new | ask | show | jobs
by milliams 519 days ago
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"!

8 comments

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

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.

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.
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.
“Carrots (^) cost dollars ($)” is a mnemonic for the order — the carrot comes first and the dollar sign comes last.
For me, it's '^' pointing up, up being before, or first if time flows from top to bottom.
But isn't the $ usually at the end of the prompt? :)
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

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.
Depends if you decide to use vim as an editor. By now it's second nature (even though I'm nowhere near a vim master and struggle) this is one thing I use often.