5

It is well known that regular expressions are powerful. But it is also known that the syntax is a bit awkward.

For example, each time I use them, I find asking myself which of ^ and $ is the start of string (respectively end of string) metacharacter.

I know that a quick google search or a simple cheat sheet may answer this, but yet I wonder if anyone knows a good mnemonic tip.

Yann Trevin
  • 3,823
  • 1
  • 30
  • 32

4 Answers4

5

This is what I can come up with: Starts small ends big.

holygeek
  • 15,653
  • 1
  • 40
  • 50
2

caret comes before dollar alphabetically.

wisbucky
  • 33,218
  • 10
  • 150
  • 101
1

I don't know a help for those anchors, but as long as you operate on the whole string and don't use the multi line modifier m (depends on your regex flavor) you can use in most flavors \A and \z (or \Z) for the same purpose.

Those are more intuitive to use, A us the start and Z is the end.

\A The beginning of the input
\Z The end of the input but for the final terminator, if any
\z The end of the input

stema
  • 90,351
  • 20
  • 107
  • 135
0

Late answer but I just had a talk about the exact same subject with people in various place on the internet. Here is what we found so far.

So, let’s begin with a masonry analogy. The caret (^) looks like a house roof. So let’s say that first you build the roof, then you get paid (got the $).

Similarly, people mentioned that money always comes at the end, although some thought that it was not the case in some mafia business, so this is possibly a confusing mnemonic. Variation : « you will be paid in the end ».

A simple, easy to remember one: « ^ looks like a star(t) ».

smonff
  • 3,399
  • 3
  • 36
  • 46
  • All the mnemonics come with links quoting the persoma you shared those brillant tricks. Thanks a lot to them! – smonff May 13 '23 at 08:38