Regular expression to match only strings NOT containing particular words - Unix

This is a discussion on Regular expression to match only strings NOT containing particular words - Unix ; I can write a regular expression that will only match strings that are NOT the word apple: ^([^a].*|a[^p].*|ap[^p].*|app[^l].*|apple.+)$ But is there a neater way, and how would I do it to match strings that are NOT the word apple OR ...

+ Reply to Thread
Results 1 to 5 of 5

Thread: Regular expression to match only strings NOT containing particular words

  1. Regular expression to match only strings NOT containing particular words

    I can write a regular expression that will only match strings that are
    NOT the word apple:

    ^([^a].*|a[^p].*|ap[^p].*|app[^l].*|apple.+)$

    But is there a neater way, and how would I do it to match strings that
    are NOT the word apple OR banana? Then what would be needed to match
    only strings that do not CONTAIN the word "apple" or "banana" or
    "cherry"?

    I'd love it if the following worked:

    ^[^(apple)(banana)(cherry)]*$

    But it appears the parantheses are ignored, as

    ^[(apple)(banana)(cherry)]*$

    simply matches any string that consists entire of the characters
    a,b,c,e,h,l,n,r,p & y.


  2. Re: Regular expression to match only strings NOT containing particular words

    Dylan Nicholson wrote:
    > I can write a regular expression that will only match strings that are
    > NOT the word apple:
    >
    > ^([^a].*|a[^p].*|ap[^p].*|app[^l].*|apple.+)$
    >
    > But is there a neater way, and how would I do it to match strings that
    > are NOT the word apple OR banana? Then what would be needed to match
    > only strings that do not CONTAIN the word "apple" or "banana" or
    > "cherry"?


    !(/apple/ or /banana/ or /cherry/)

    jue



  3. Re: Regular expression to match only strings NOT containing particular words

    2007-10-18, 22:00(-07), Dylan Nicholson:
    > I can write a regular expression that will only match strings that are
    > NOT the word apple:
    >
    > ^([^a].*|a[^p].*|ap[^p].*|app[^l].*|apple.+)$
    >
    > But is there a neater way, and how would I do it to match strings that
    > are NOT the word apple OR banana? Then what would be needed to match
    > only strings that do not CONTAIN the word "apple" or "banana" or
    > "cherry"?
    >
    > I'd love it if the following worked:
    >
    > ^[^(apple)(banana)(cherry)]*$
    >
    > But it appears the parantheses are ignored, as
    >
    > ^[(apple)(banana)(cherry)]*$
    >
    > simply matches any string that consists entire of the characters
    > a,b,c,e,h,l,n,r,p & y.


    With perl regexps:

    perl -ne 'print if /^(??!apple|banana).)*$/'
    or probably better:
    perl -ne 'print if /^(?!.*(?:apple|banana))/'

    But then, why not

    perl -ne 'print if !/apple|banana/'

    Note that vim's regexps have an equivalent negative look-ahead
    operator.

    --
    Stéphane

  4. Re: Regular expression to match only strings NOT containing particular words

    On Thu, 18 Oct 2007 22:00:28 -0700, Dylan Nicholson
    wrote:

    >I can write a regular expression that will only match strings that are
    >NOT the word apple:
    >
    >^([^a].*|a[^p].*|ap[^p].*|app[^l].*|apple.+)$
    >
    >But is there a neater way, and how would I do it to match strings that
    >are NOT the word apple OR banana? Then what would be needed to match
    >only strings that do not CONTAIN the word "apple" or "banana" or
    >"cherry"?
    >
    >I'd love it if the following worked:
    >
    >^[^(apple)(banana)(cherry)]*$
    >
    >But it appears the parantheses are ignored, as
    >
    >^[(apple)(banana)(cherry)]*$
    >
    >simply matches any string that consists entire of the characters
    >a,b,c,e,h,l,n,r,p & y.


    A simple way is to write the regex to match apple or banana or cherry,
    do the match and then check the Success property of the match object.

    Execute the following mini program

    using System;
    using System.Collections.Generic;
    using System.Text.RegularExpressions;

    namespace ConsoleApplication1
    {
    class Program
    {
    static void Main(string[] args)
    {
    Regex r = new Regex(".*apple|banana|cherry.*");
    string[] strings =
    "apple,banana,cherry,applebanana,applebananacherry, fishapple,chips,chip
    and apple,apple pie".Split(',');
    foreach (string s in strings)
    {
    Console.WriteLine("{0} Match? {1}", s,
    r.Match(s).Success);
    }
    Console.ReadLine();
    }
    }
    }

    You should get this:

    apple Match? True
    banana Match? True
    cherry Match? True
    applebanana Match? True
    applebananacherry Match? True
    fishapple Match? True
    chips Match? False
    chip and apple Match? True
    apple pie Match? True

    --
    http://bytes.thinkersroom.com

  5. Re: Regular expression to match only strings NOT containing particular words

    On Thu, 18 Oct 2007 22:00:28 -0700, Dylan Nicholson
    wrote:

    >But is there a neater way, and how would I do it to match strings that
    >are NOT the word apple OR banana? Then what would be needed to match
    >only strings that do not CONTAIN the word "apple" or "banana" or
    >"cherry"?


    The general answer is that you should use separate regexen and logical
    operators, or an explicit !~ but the subject of negating regexen is
    discussed to some depth in the following thread @ PM:

    http://perlmonks.org/?node_id=588315


    Michele
    --
    {$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
    (($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^ ..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER 256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,

+ Reply to Thread