Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Question about regex.

by haukex (Bishop)
on Sep 29, 2020 at 18:11 UTC ( #11122346=note: print w/replies, xml ) Need Help??


in reply to Question about regex.

A couple of points that haven't been mentioned yet:

  • Perl's input record separator $/ has a special "paragraph mode" when you set local $/=""; that will split the input on one or more blank lines.
  • GrandFather mentioned the spaces in the regex. If you wanted to format your regex nicely and have whitespace ignored, you could use the /x modifier (perlre).
  • Cristoforo's suggestion is nice and short, but has the disadvantage that it reads the entire file into memory before grepping it.

Replies are listed 'Best First'.
Re^2: Question about regex.
by GrandFather (Saint) on Sep 29, 2020 at 20:11 UTC
    "local $/="";
    "If you wanted to format your regex nicely and have whitespace ignored, you could use the /x modifier"

    Anyone else feel a dissonance here? I'd format the assignment (yes, it is an assignment) as:

    local $/ = "";

    On first glance the assignment looks like $/= "" to me and that's not right. An eye blink later it turns into $ /= "" which doesn't work either. It's only after two glances and a hard look that it resolves into $/ = "";. Using white space to reduce cognitive load is a Good Thing

    Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond
      Anyone else feel a dissonance here?

      Yes, you do have a point, I didn't need to be stingy with those two spaces. Though I did say "If you wanted to format your regex nicely", and not "you should format your regex nicely" (and noone else in the thread used /x either) ;-)

      Edit: missed a word on c&p

        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

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (7)
As of 2021-06-24 17:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What does the "s" stand for in "perls"? (Whence perls)












    Results (130 votes). Check out past polls.

    Notices?