Mnemonic tip for regex start of string/end of string metacharacters
Asked Answered
D

4

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.

Dunham answered 29/7, 2011 at 10:31 Comment(0)
P
5

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

Pudendas answered 29/7, 2011 at 10:54 Comment(0)
Q
2

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

Quarters answered 29/7, 2011 at 10:53 Comment(0)
H
2

caret comes before dollar alphabetically.

Hartnett answered 2/8, 2017 at 23:6 Comment(0)
A
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) ».

Acaroid answered 13/5, 2023 at 8:34 Comment(1)
All the mnemonics come with links quoting the persoma you shared those brillant tricks. Thanks a lot to them!Acaroid

© 2022 - 2024 — McMap. All rights reserved.