Go Back   CodingForums.com > :: Server side development > Perl/ CGI

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-24-2002, 03:20 PM   PM User | #1
j-dogg
New to the CF scene

 
Join Date: Sep 2002
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
j-dogg is an unknown quantity at this point
Matching "anything but" a string

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.
j-dogg is offline   Reply With Quote
Old 09-25-2002, 05:19 PM   PM User | #2
Mouldy_Goat
Regular Coder

 
Join Date: Jul 2002
Location: London, UK
Posts: 126
Thanks: 0
Thanked 0 Times in 0 Posts
Mouldy_Goat is an unknown quantity at this point
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:

Code:
#!/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.
Mouldy_Goat is offline   Reply With Quote
Old 08-28-2007, 07:57 PM   PM User | #3
I_Love_Privacy
Banned

 
Join Date: Aug 2007
Posts: 39
Thanks: 6
Thanked 0 Times in 0 Posts
I_Love_Privacy is an unknown quantity at this point
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.
I_Love_Privacy is offline   Reply With Quote
Old 08-31-2007, 12:40 PM   PM User | #4
daveyand
New Coder

 
Join Date: Jun 2007
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
daveyand is an unknown quantity at this point
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 ...
__________________
PHP / CSS / Perl / XML / XHTML

For cheap ipod touch go to protella.com
daveyand is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:46 PM.


Advertisement
Log in to turn off these ads.