in reply to quick regex help for multiple OR

my $regex = join '|', qw( red green blue ); foreach my $line ( @lines ) { next if $line !~ $regex; }

Update: toolic pointed out that the conditions were different for the different colors. A better version would be:

my $negative_regex = join '|', qw( red blue ); my $positive_regex = join '|', qw( green ); foreach my $line ( @lines ) { next if $line =~ $positive_regex || $line !~ $negative_regex; }

My criteria for good software:
  1. Does it work?
  2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?

Replies are listed 'Best First'.
Re^2: quick regex help for multiple OR
by convenientstore (Pilgrim) on May 13, 2008 at 04:11 UTC
    thanks Dragonchild, this looks like good solution.

    Didn't realize you can do that w/ regex
    Just wanted to add that, OP should be using below as well.
    next unless $line =~ $regex;
      s/should/could/;

      That's a matter of personal preference.
      In PBP, TheDamian recommends avoiding the use of unless. Mainly because statements can very quickly become quite difficult to read if you start extending them and adding multiple conditions.

      I tend to agree with this view, but as I say - it's really a matter of personal preference.

      Cheers,
      Darren :)