Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Mnemonic for begin/end chars in regexes

by Smonff (Scribe)
on May 12, 2023 at 10:14 UTC ( [id://11152116]=perlquestion: print w/replies, xml ) Need Help??

Smonff has asked for the wisdom of the Perl Monks concerning the following question:

Twelve years I started with Perl and I still can't remember EVERY TIME which one between ^ and $ anchor should be used to match begin and end of regex pattern 🤔

Any mnemonic or memory aid? I can use a cheat sheet, but come on…

Thanks.

Note: this is a cross-post from plush.city

🌸

Replies are listed 'Best First'.
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.

      It was exactly what I was searching for! Thank you so much, this is very helpful.

      🌸

      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)

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.


    🦛

      I like this one, but I have to admit I would be afraid it could confuse later readers, assuming it is less common than the ^/$ form?

      🌸

        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.


        🦛

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)

    Cheers Rolf
    (addicted to the 𐍀𐌴𐍂𐌻 Programming Language :)
    Wikisyntax for the Monastery

      Very interesting, thanks. Unfortunately, you can see I totally miss this kind of logic.

      🌸
        > Unfortunately, you can see I totally miss this kind of logic.

        Interesting, how do you read /$var/?

        Cheers Rolf
        (addicted to the 𐍀𐌴𐍂𐌻 Programming Language :)
        Wikisyntax for the Monastery

Re: Mnemonic for begin/end chars in regexes
by kcott (Archbishop) on May 12, 2023 at 10:30 UTC
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 ;-)
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

      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.


      🦛

        I'd be thinking of the Simpsons version and wondering what sugar matched . . .

        The cake is a lie.
        The cake is a lie.
        The cake is a lie.

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.
      Yeah, I'm just used to using it in *nix systems generally, eg vi/vim's matching for search-&-replace .

      Hence it's now ingrained in my head/fingers :)

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.

      I think of '^' as indicating "the top", and '$' as the "bottom line".
Re: Mnemonic for begin/end chars in regexes
by Smonff (Scribe) on May 13, 2023 at 08:08 UTC
      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 ... ;)

      Cheers Rolf
      (addicted to the 𐍀𐌴𐍂𐌻 Programming Language :)
      Wikisyntax for the Monastery

        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.

        🌸

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11152116]
Approved by marto
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (5)
As of 2024-04-19 20:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found