Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Sep 2002
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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.

  • #2
    Regular Coder
    Join Date
    Jul 2002
    Location
    London, UK
    Posts
    126
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #3
    Banned
    Join Date
    Aug 2007
    Posts
    39
    Thanks
    6
    Thanked 0 Times in 0 Posts
    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.

  • #4
    New Coder
    Join Date
    Jun 2007
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •