View Full Version : Matching "anything but" a string

09-24-2002, 03:20 PM
Hi. I'm trying to make a regular expression, part of which will match anything but a certain string. I know you can match "anything but" a character class in [] using ^. But I want to match anything but an alternate expression (ie a string) in () brackets. Is there a way to do this?

Thanks in advance.

09-25-2002, 05:19 PM
Ok, I'm not sure if this is what you mean, but I knocked up an example of the sort of thing I think you're after:


$string = "lalala monkey brain foo bar brain baz";

($no_brain = $string) =~ s/brain//ig;
This is basically just a way of capturing a version of $string into the $no_brain variable, but without any 'brain's. It works by taking a copy of $string into $no_brain and then deleting any instances of the word brain from it.

Tell me if this isn't what you're after..
Hope it helps a bit anyway.

08-28-2007, 07:57 PM
Wow, talk about slow response....
Anyway, I just found out the answer to this one, so I thought somebody else may appreciate it:

"Insane in the membrane" ~= /(?!brain).*/
"Insane in the membrane" ~= /(?!brane).*/
"Insane in the membrane" ~= /(?!brain|brane).*/


08-31-2007, 12:40 PM
you could also do the following:

blah ~! !brain!;

I think that sthe right syntax, instead of doing an = it does a not equal.

so an example

$string = "brain";

if($string !~ /brain/) {}

Or something like that anyway ...

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum