in reply to RegEx to match at least one non-adjacent term

I know you are looking for a regex solution, but just in case you change your mind, maybe something like this would suit your needs...

Updated: to handle parentheses.

#!/usr/bin/env perl use warnings; use strict; while (<DATA>) { #my @no_reds = grep { !/^r/i } split; my @no_reds = grep { !/^r/i } split /[\s\(\)]/; print "@no_reds\n"; } __DATA__ 12345 Gray 6789 Red rd 555 blue 888 red 777 green 999 foo bar r baz ruby goo(r) ruby doobie 888red999

Prints out:

12345 Gray 6789 555 blue 888 777 green 999 foo bar baz goo doobie 888red999

Unlike ikegami's solution, this requires the input to be whitespace-separated.

Replies are listed 'Best First'.
Re^2: RegEx to match at least one non-adjacent term
by Cefu (Beadle) on Dec 07, 2007 at 17:12 UTC

    Thanks for the idea. It's always good to break out of my assumption that RegEx is the right way and consider something else like Grep.

    Unfortunately, this was a free-text field filled by users, so I can't assume they'll give me whitespacea, any other convention or even the results of normal human brain function.

    Also, this method is either a little lacking or it's way TOO smart if it knows that rubies are actually red. :)