Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Re^5: Question about regex.

by hippo (Chancellor)
on Sep 30, 2020 at 09:03 UTC ( #11122360=note: print w/replies, xml ) Need Help??

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

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.


Replies are listed 'Best First'.
Re^6: Question about regex.
by haukex (Bishop) on Sep 30, 2020 at 09:59 UTC
    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?

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (3)
As of 2021-06-21 01:44 GMT
Find Nodes?
    Voting Booth?
    What does the "s" stand for in "perls"? (Whence perls)

    Results (97 votes). Check out past polls.