[thelist] Reg Ex - everything except a phrase

David Kaufman david at gigawatt.com
Wed Sep 10 08:05:28 CDT 2003


Mark M <mark.m at optushome.com.au> wrote:
> Hey guys - this is driving me nuts -
>
> I'm wondering if there is a way I can do a check on everything except
> a phrase.
>
> [...] Is this possible, or am I approaching this the wrong way?

yes to both :-)


> I.e. - you can say: "[^abc]" - which is everything except 'a', 'b',
> or 'c'.
>
> What I want to be able to do is something like -
>
> "[^(fred)]" - which would be everything except for the phrase 'fred'
> rather than everything except 'f', 'r', 'e', or 'd'.

the parens around fred will not do what you want them to inside the
[square brackets] because they just define a "character class".  the
[^(fred)] class will simply match one of any letter that isn't f, r,
e, d, (, or ).

you don't mention the language your using, but in perl, to match
everything other than fred, in a string, you might try thinking of it as
matching "everything before fred plus everything after fred" (if you
only expect one fred, that is).

frinstance, to exclude fred from these three cases:

case a:

  my $a = 'i am not fred'; # fred at the end
  $a =~ s/^(.*)(?:fred)(.*)$/$1$2/; print $a, "\n";

prints "i am not "


case 2:

  my $b = 'fred is not i'; # fred at the beginning
  $b =~ s/^(.*)(?:fred)(.*)$/$1$2/; print $b, "\n";

prints " is not i"


case iii:
  my $c = 'i, not fred, am me';
  $c =~ s/^(.*)(?:fred)(.*)$/$1$2/; print $c, "\n";
prints "i, not , am me"


if, however fred may pop up *multiple* times in the string, notice that
this simple technique excludes only the *last* fred:

case multifred:

  my $d = 'i, not fred, am me, whilst fred is not!';
  $d =~ s/^(.*)(?:fred)(.*)$/$1$2/; print $d, "\n";

prints "i, not fred, am me, whilst  is not!",

thanks to greedy matching.  if you need to match everything except
multiple freds, a simple loop is usually best:

  $d =~ s/^(.*)(?:fred)(.*)$/$1$2/ while $d =~ /fred/; print $d, "\n";

prints "i, not , am me, whilst  is not!"

hth,

-dave






More information about the thelist mailing list