http://qs321.pair.com?node_id=11122360


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 (Archbishop) 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.