Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re^4: Question about regex.

by GrandFather (Saint)
on Sep 30, 2020 at 00:45 UTC ( [id://11122354]=note: print w/replies, xml ) Need Help??


in reply to Re^3: Question about regex.
in thread Question about regex.

I get a bit touchy about white space. Many of my work mates eschew spaces wherever possible and at the other extreme I see stuff like function( param1 , param2 , param3 ); which hurts my brain almost as much as no spaces. My rule of thumb is to use white space where possible as you would for prose on the basis that that is what we are most used to parsing so we get a fair bit of the parsing work essentially for free.

Interestingly regexen are something of an exception for me. Unless they are fairly complicated I don't use /x. When I do use /x I often break the regex up over several lines and comment each part. Mostly though I keep the regexen fairly simple and do complicated stuff where it's easier to maintain.

Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond

Replies are listed 'Best First'.
Re^5: Question about regex.
by haukex (Archbishop) on Sep 30, 2020 at 07:17 UTC
    My rule of thumb is to use white space where possible as you would for prose on the basis that that is what we are most used to parsing so we get a fair bit of the parsing work essentially for free.

    I also tend to use whitespace around operators and after commas, but at the same time, personally I can read things like $/=""; easily, which is why I don't always put whitespace around operators if the expression is short - if I'm coding for myself, I just base it on my feeling of readability. If I had to make a choice for a best practice recommendation though, I'd say whitespace around operators is good.

    Interestingly regexen are something of an exception for me.

    For me, regexes don't always read easily, especially if there's a lot of nested parens going on, so when trying to understand a regex, I'll often add the /x and whitespace.

Re^5: Question about regex.
by hippo (Bishop) on Sep 30, 2020 at 09:03 UTC

    Your post exactly matches my own approach.

    Interestingly regexen are something of an exception for me. Unless they are fairly complicated I don't use /x.

    I've been thinking about this because it is, as you say, the exception. My conclusion is that often enough a regex will contain a short section of prose from somewhere else and it is trivial to paste this in from the other source. With /x one would then have to go through it manually and escape all the spaces or replace them with something else. The problem of course compounds with interpolation:

    my $pre = 'Put the needle on the record'; my $post = 'the drum beats go like'; if ($corpus =~ /($pre\W*){3}when $post this!/) { $volume++; }

    Good luck managing that with /x - far better without. When the regex starts to become too large/unwieldy I just split it into several shorter ones which are then combined which I find helps the logical flow too.

    For completeness: the one operator I do sometimes cuddle with operands is the range operator but that's likely a relic from some shells (such as bash) where whitespace around the same construct can cause problems.


    🦛

      Good luck managing that with /x

      m{ ( \Q$pre\E \W* ){3} \Qwhen $post this!\E }x isn't too horrible, IMHO, but your point of breaking it up into several qr//s is of course good too.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11122354]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (5)
As of 2024-04-19 06:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found