regex problem

K

km_jr_usenet

Hi all,

I am new to Java. I want to use regex to match against some strings.
One of the requirement is to match a string if it is NOT equal to some
other string. I know one way would be to negate the result but I want
to know an alternate solution. Here is the sample code that shows my
problem -

import java.util.regex.Matcher;
import java.util.regex.Pattern;

class test {

public static void main(String args[]) {
Matcher m1, m2, m3;
String firstPattern = "^cat$";
String secondPattern = "???"; // Pattern should match
everything except "dog"

m1 = Pattern.compile(firstPattern).matcher("cat"); // true
m2 = Pattern.compile(secondPattern).matcher("dog"); //
should be false
m3 = Pattern.compile(secondPattern).matcher("cat"); //
should be true


}
}
 
J

Joshua Cranmer

Hi all,

I am new to Java. I want to use regex to match against some strings.
One of the requirement is to match a string if it is NOT equal to some
other string. I know one way would be to negate the result but I want
to know an alternate solution. Here is the sample code that shows my
problem -

"^(?!dog)" should work, as was once suggested by another person on this
list a few years back.
 
C

Claudio Nieder

Hi,
string. I know one way would be to negate the result but I want to know
an alternate solution. Here is the sample code that shows my problem -

I'd say not possible as AFAIK there is no negation operation within
regular expressions except for single character in e.g. [^ac] which means
match any character except a or c.
m2 = Pattern.compile(secondPattern).matcher("dog"); //
should be false

I'd obviously write isNotDog=!mystring.equals("dog");

What problem do you try to solve?

claudio
 
K

km_jr_usenet

"^(?!dog)" should work, as was once suggested by another person on this
list a few years back.

Doesn't seem to work with Java 1.6

class test2 {

public static void main(String args[]) {
Matcher m1, m2, m3;
String firstPattern = "^cat$";
String secondPattern = "^(?!dog)";

m1 = Pattern.compile(firstPattern).matcher("cat"); // true
m2 = Pattern.compile(secondPattern).matcher("dog"); //
m3 = Pattern.compile(secondPattern).matcher("cat"); //
System.out.println(m1.matches());
System.out.println(m2.matches());
System.out.println(m3.matches());
[~]$ java -classpath . test2
true
false
false

(see the 3rd o/p)

Thanks..
}
}
 
C

Claudio Nieder

Hi,
"^(?!dog)" should work, as was once suggested by another person on this
list a few years back.

That will return false also for words starting with dog:

final Pattern notDog=Pattern.compile("^(?!dog)");
Matcher m=notDog.matcher("cat");
System.out.println(m.find());
m=notDog.matcher("dog");
System.out.println(m.find());
m=notDog.matcher("dogfood");
System.out.println(m.find());
System.exit(2);

gives

true
false
false

claudio
 
K

km_jr_usenet

Hi,
string. I know one way would be to negate the result but I want to know
an alternate solution. Here is the sample code that shows my problem -

I'd say not possible as AFAIK there is no negation operation within
regular expressions except for single character in e.g. [^ac] which means
match any character except a or c.
m2 = Pattern.compile(secondPattern).matcher("dog"); //
should be false

I'd obviously write isNotDog=!mystring.equals("dog");

What problem do you try to solve?

claudio


We have a set of strings to match against. Regex seemed like the way
to go. There was also a set of requirement to exclude certain strings
during the match process. I asked the question because I couldn't find
any way to do this.
 
R

Roedy Green

I am new to Java. I want to use regex to match against some strings.
One of the requirement is to match a string if it is NOT equal to some
other string. I know one way would be to negate the result but I want
to know an alternate solution. Here is the sample code that shows my
problem -

Just match and use the result boolean in the opposite sense.
see http://mindprod.com/jgloss/regex.html
 
C

Claudio Nieder

Hi,
We have a set of strings to match against. Regex seemed like the way to
go. There was also a set of requirement to exclude certain strings
during the match process. I asked the question because I couldn't find
any way to do this.

If I had to do it, I wouldn't bother long with finding a complex pattern
which would match on both wanted and unwanted texts, but rather have two
Patterns, one which matches everything wanted and one which matches
everything unwanted and then perform

stringAccepted=wantedMatcher.matches()&&!undesiredMatcher.matches();

claudio
 
K

km_jr_usenet

Hi,


If I had to do it, I wouldn't bother long with finding a complex pattern
which would match on both wanted and unwanted texts, but rather have two
Patterns, one which matches everything wanted and one which matches
everything unwanted and then perform

stringAccepted=wantedMatcher.matches()&&!undesiredMatcher.matches();

That is a good suggestion. It seems better than writing some complex
regex expression.

Thanks!
 
C

Claudio Nieder

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Pattern and Regex 7
regexp(ing) Backus-Naurish expressions ... 23
Regex: Capturing and replacing question 9
School Project 1
RegEx problem 5
regex 12
newbie Java regexp question 4
regex question 4

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top