Re: regex problem



Hi,

The regular expression to do what you requested is simple borderline
trivial. There really is nothing complex about it, unless you are not

Well, in the exaples which have been posted here we had just seen how to
have one regular expression not match the word dog. But not yet, how to
write one which would match if the sentnece contains cat unless it also
contains dog.

To get the result

"I have no pets" ==> false
"I have a cat as pet" ==> true
"I have a dog at home" ==> false
"I got a cat and a dog" ==> false
"My dog hates my cat" ==> false

I'd simply write:

import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class R
{
final static Pattern cat=Pattern.compile("cat");
final static Pattern dog=Pattern.compile("dog");

static void catWithoutDog(final String s)
{
final Matcher catMatcher=cat.matcher(s);
final Matcher dogMatcher=dog.matcher(s);
System.out.println("\""+s+"\"\t==> "+(catMatcher.find()&&!
dogMatcher.find()));
}

public static void main(final String[] args)
{
catWithoutDog("I have no pets");
catWithoutDog("I have a cat as pet");
catWithoutDog("I have a dog at home");
catWithoutDog("I got a cat and a dog");
catWithoutDog("My dog hates my cat");
}
}

How would a single regular expression for this look like?

claudio
--
Claudio Nieder, Talweg 6, CH-8610 Uster, Tel +4179 357 6743,
www.claudio.ch
.



Relevant Pages

  • Re: Petsmart website re food recall
    ... Cat AUTHORITY 73725737358 OCEAN FISH CUTS IN SAUCE CAT FOOD 6 Ounce ... Cat IAMS 1901400530 SELECT BITES BEEF CAT FOOD 3 OZ 3 Ounce ... Dog AUTHORITY 73725712043 CHICKEN & RICE CUTS IN GRAVY SR DOG 14 Ounce ...
    (sci.med.transcription)
  • Re: Chinese Crested Lover
    ... HOWEDY diddler, ... diddy wrote: ... "My Husband Shot A Dog That Had Been Tearing Up ... But mom and dad now have a house cat, ...
    (rec.pets.dogs.behavior)
  • AMERICAS REIGNING CATS AND DOGS - ELECTION 2008
    ... praising and glorifying God and his wonderful new ... But a little red mini-me serpent with horns and a tail appeared ... the serpent found the first dog and cat. ...
    (soc.culture.usa)
  • AMERICAS REIGNING CATS AND DOGS - ELECTION 2008
    ... praising and glorifying God and his wonderful new ... But a little red mini-me serpent with horns and a tail appeared ... the serpent found the first dog and cat. ...
    (talk.politics.misc)
  • AMERICAS REIGNING CATS AND DOGS - ELECTION 2008
    ... praising and glorifying God and his wonderful new ... But a little red mini-me serpent with horns and a tail appeared ... the serpent found the first dog and cat. ...
    (alt.politics.bush)