Regex to not match a string

Discussion in 'Java' started by Neil, Aug 29, 2009.

  1. Neil

    Neil Guest

    Hello:

    I have come up with a scenario where I need to form a regex that does
    not match
    either of two strings.

    For example, I want the regex to match anything besides cat and dog,
    horse
    should yield a match, pig should yield a match, etc.

    I tried this:
    [^(cat|dog)]

    but that did not work.

    Any ideas?

    Thanks,
    Neil

    --
    Neil Aggarwal, (281)846-8957, www.JAMMConsulting.com
    Will your e-commerce site go offline if you have
    a DB server failure, fiber cut, flood, fire, or other disaster?
    If so, ask about our geographically redundant database system.
     
    Neil, Aug 29, 2009
    #1
    1. Advertising

  2. Neil wrote:
    > Hello:
    >
    > I have come up with a scenario where I need to form a regex that does
    > not match
    > either of two strings.
    >
    > For example, I want the regex to match anything besides cat and dog,
    > horse
    > should yield a match, pig should yield a match, etc.
    >
    > I tried this:
    > [^(cat|dog)]
    >
    > but that did not work.
    >
    > Any ideas?


    (?!cat|dog) should work

    The (?!) construct means a negative lookahead: it matches iff the regex
    in the group does not match.

    --
    Beware of bugs in the above code; I have only proved it correct, not
    tried it. -- Donald E. Knuth
     
    Joshua Cranmer, Aug 29, 2009
    #2
    1. Advertising

  3. Neil

    Roedy Green Guest

    On Fri, 28 Aug 2009 17:59:31 -0700 (PDT), Neil
    <> wrote, quoted or indirectly quoted someone
    who said :

    >Hello:
    >
    >I have come up with a scenario where I need to form a regex that does
    >not match
    >either of two strings.


    One way out is to write a regex to find either string then invert the
    boolean result of matches
    --
    Roedy Green Canadian Mind Products
    http://mindprod.com

    "Any one who considers arithmetical methods of producing random digits is, of course, in a state of sin. For, as has been pointed out several times, there is no such thing as a random number — there are only methods to produce random numbers, and a strict arithmetic procedure of course is not such a method."
    ~ John von Neumann (born: 1903-12-28 died: 1957-02-08 at age: 53)
     
    Roedy Green, Aug 29, 2009
    #3
  4. Joshua Cranmer <> wrote:
    >> I have come up with a scenario where I need to form a regex that does
    >> not match either of two strings.
    >>
    >> For example, I want the regex to match anything besides cat and dog,
    >> horse should yield a match, pig should yield a match, etc.

    > (?!cat|dog) should work The (?!) construct means a negative lookahead...


    There's also a way to construct "conventional" regular expressions, as long
    as you can anchor the starting point of the not-occurrance of these words:
    e.g.:
    ^([^cd]|c[^a]|d[^o]|ca[^t]|do[^g])

    It quickly becomes messy as the not-to-occurrants become longer, and also
    messier (but not so much), if you e.g. still want the catfish to pass the re.


    > --
    > Beware of bugs ...

    heh, I read "Beware of the dogs ..." this time :)
     
    Andreas Leitgeb, Aug 29, 2009
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. hiwa
    Replies:
    0
    Views:
    653
  2. Replies:
    3
    Views:
    822
    Reedick, Andrew
    Jul 1, 2008
  3. Old Echo
    Replies:
    1
    Views:
    199
    Adam Shelly
    Sep 4, 2008
  4. Ruby Newbee

    regex =~ string or string =~ regex?

    Ruby Newbee, Jan 4, 2010, in forum: Ruby
    Replies:
    3
    Views:
    148
    Kirk Haines
    Jan 4, 2010
  5. Replies:
    4
    Views:
    129
    Tad McClellan
    Oct 4, 2005
Loading...

Share This Page