...

View Full Version : Matching "anything but" a string



j-dogg
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.

Mouldy_Goat
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:


#!/usr/bin/perl

$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.

I_Love_Privacy
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).*/
(TRUE)
"Insane in the membrane" ~= /(?!brane).*/
(NO MATCH)
"Insane in the membrane" ~= /(?!brain|brane).*/
(NO MATCH)

Enjoy.

daveyand
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