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
}
}
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
}
}