Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Regex to not match a string

Reply
Thread Tools

Regex to not match a string

 
 
Neil
Guest
Posts: n/a
 
      08-29-2009
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.
 
Reply With Quote
 
 
 
 
Joshua Cranmer
Guest
Posts: n/a
 
      08-29-2009
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
 
Reply With Quote
 
 
 
 
Roedy Green
Guest
Posts: n/a
 
      08-29-2009
On Fri, 28 Aug 2009 17:59:31 -0700 (PDT), Neil
<(E-Mail Removed)> 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)
 
Reply With Quote
 
Andreas Leitgeb
Guest
Posts: n/a
 
      08-29-2009
Joshua Cranmer <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

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
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
regex =~ string or string =~ regex? Ruby Newbee Ruby 3 01-04-2010 06:04 PM
String#match vs. Regexp#match - confused Old Echo Ruby 1 09-04-2008 06:11 PM
How make regex that means "contains regex#1 but NOT regex#2" ?? seberino@spawar.navy.mil Python 3 07-01-2008 03:06 PM
RegEx How to not match a string jim_adams@hotmail.com Perl Misc 4 10-04-2005 05:10 AM
Java regex can't match lengthy match? hiwa Java 0 01-29-2004 10:09 AM



Advertisments