Re: regex problem
- From: Claudio Nieder <private@xxxxxxxxxx>
- Date: Sat, 30 Aug 2008 03:29:26 +0200
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
.
- Follow-Ups:
- Re: regex problem
- From: jamil
- Re: regex problem
- References:
- Re: regex problem
- From: Claudio Nieder
- Re: regex problem
- From: km_jr_usenet
- Re: regex problem
- Prev by Date: remote file date
- Next by Date: Re: exceptions: checked or unchecked?
- Previous by thread: Re: regex problem
- Next by thread: Re: regex problem
- Index(es):
Relevant Pages
|