Re: Mnemonic for begin/end chars in regexes - roof and money
by Discipulus (Canon) on May 12, 2023 at 11:11 UTC
|
Bonjour Smonff,
I always used a masonry analogy: you first start building the roof ^ and only at the end you get paid $
Now that I see it it can be semplified as: for any work you do money come as last
L*
There are no rules, there are no thumbs..
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
| [reply] [d/l] [select] |
|
| [reply] |
|
start building the roof ^ and only at the end you get paid $
That's great and ^ certainly is the roof, while $ to me looks like a fishing hook, down in the murky waters :-)
Edited to add: ...the fishing hook of course marks the end of the line, fishing line, yes.
Cheers, Sören
Créateur des bugs mobiles - let loose once, run everywhere.
(hooked on the Perl Programming language)
| [reply] |
Re: Mnemonic for begin/end chars in regexes
by hippo (Bishop) on May 12, 2023 at 12:49 UTC
|
Since nobody has mentioned it yet, you can just use the synonyms of \A for the start and \Z for the end. That's about as intuitive as you can get, I would think. The only difference comes if you use the /m modifier. See perlre for the details.
| [reply] [d/l] [select] |
|
| [reply] |
|
I'm pretty sure that usage of \A and \Z is less common than usage of ^ and $. But is usage of \A and \Z also less common than confusion of ^ and $? Probably not.
If you want to keep other folks in their comfort zone, stick to ^ and $ and learn which is which. If you are just using your own regexen day-to-day on the CLI then use whatever makes you more comfortable.
| [reply] |
Re: Mnemonic for begin/end chars in regexes
by LanX (Saint) on May 12, 2023 at 12:59 UTC
|
Not exactly a mnemonic, but logical thinking
Perl regexes allow variable interpolation and a final $ is taken as a meta for end-of-line
- /^\Q$string$/ tests $_ eq $string
Hence a $ at the beginning would be very impracticable
edit
Otherwise, what would /$var/ be supposed to match?
- interpolation of $var as regex? (that's what's happening)
- literal characters "var" at start-of-line? (nope)
| [reply] [d/l] [select] |
|
| [reply] |
|
| [reply] [d/l] |
Re: Mnemonic for begin/end chars in regexes
by kcott (Archbishop) on May 12, 2023 at 10:30 UTC
|
G'day Smonff,
I suggest you bookmark these perlre sections:
Perhaps you can write your own cheat sheet based on the information there.
| [reply] |
Re: Mnemonic for begin/end chars in regexes
by soonix (Canon) on May 12, 2023 at 10:20 UTC
|
I know some prefer writing amounts in the form "$ 42", but probably more natural is "42 $". The caret (in some character sets an up arrow) might be seen as the hook from which the line hangs ;-) | [reply] |
Re: Mnemonic for begin/end chars in regexes
by bliako (Monsignor) on May 12, 2023 at 11:44 UTC
|
For me it's (first) power (^) and (then) money ($). Although the reverse is also possible, depending your state corruption flavour. For those reminiscent of the glorious past it could be the power of the dollar.
bw, bliako
| [reply] [d/l] [select] |
|
Unfortunately for me the phrase regarding which comes first between money and power is always the Scarface quote, so the money would come first.
Luckikly not a day goes by that I don't work with some regex or another so these symbols are just ingrained by now.
| [reply] |
|
| [reply] |
|
Re: Mnemonic for begin/end chars in regexes
by soonix (Canon) on May 13, 2023 at 13:38 UTC
|
Among the answers to your crosspost on the fediverse, Mark Gardner points out that this syntax is of similiar age as Unix, definitely older than Perl. | [reply] |
|
| [reply] |
Re: Mnemonic for begin/end chars in regexes
by linuxer (Curate) on May 12, 2023 at 18:03 UTC
|
I thought of something like this:
I begin reading at the top of a page, so I see ^ pointing to the beginning of the text on a page, or here to the beginning of the string.
Currently I don't have a similar picture for the $ at the end. But this decision is a 50/50 chance, so if I can memorize the ^ for the beginning, the other must be for the end.
| [reply] |
|
I think of '^' as indicating "the top", and '$' as the "bottom line".
| [reply] |
Re: Mnemonic for begin/end chars in regexes
by Smonff (Scribe) on May 13, 2023 at 08:08 UTC
|
Regular expressions are perfect, like the Garden of Eden.
Snakes end the Garden of Eden.
The $ looks like a snake.
| [reply] [d/l] |
|
To complete your Eden analogy:
^
/👁\
The caret reminds me of a floating triangle.
The Eye of Providence is a symbol for God, "the beginning of all".
edit
Depicted on millions of $-bills ... ;)
| [reply] |
|
It looks like a very Perlish interpretation indeed, possibly one of the most appropriate. I’ll keep digging into this. It also nicely links to the masonry mnemonic that @Discipulus gave, the roof caret and pay bill in the end.
| [reply] |
|
|
|
| [reply] |