N
News
Hello all,
I want to create a logic class to evaluate simple logical epxressions and
print their truth table. I am using a regular epxression that looks for a
pattern commencing with a char and followed by any number of (operator char)
groups, (for the sake of simplicity only the AND operator "&" is included
till I get it working properly).
My regex is [a-b]([&][a-b])*. I know the regex is correct because I have
tested it using the regular expression demo from
www.regular-expressions.info .
Following is my code stripped to the essentials. As it stands this returns a
match for even misformed strings and I cannot see why !
import java.util.regex.*;
public class Logic {
public static void main(String[] args) {
StringBuffer strb = new StringBuffer();
for (int i = 0; i < args.length; i++) {
strb.append(args); //Add the command line arguments to String Buffer }
String str = strb.toString(); //Change to a string so Matcher can use it.
String regex = new String("[a-z]([&][a-z])*");
System.out.println(str); //Test print to ensure the string and regex are
correct
System.out.println(regex);
Pattern p = Pattern.compile(regex, Pattern.CASE_INSENSITIVE |
Pattern.UNICODE_CASE);
Matcher m = p.matcher(regex);
if (m.find()) {
System.out.println("Matched"); }
else {
System.out.println("Not Matched"); }
}
}
Any ideas ? Thanks in advance !
I want to create a logic class to evaluate simple logical epxressions and
print their truth table. I am using a regular epxression that looks for a
pattern commencing with a char and followed by any number of (operator char)
groups, (for the sake of simplicity only the AND operator "&" is included
till I get it working properly).
My regex is [a-b]([&][a-b])*. I know the regex is correct because I have
tested it using the regular expression demo from
www.regular-expressions.info .
Following is my code stripped to the essentials. As it stands this returns a
match for even misformed strings and I cannot see why !
import java.util.regex.*;
public class Logic {
public static void main(String[] args) {
StringBuffer strb = new StringBuffer();
for (int i = 0; i < args.length; i++) {
strb.append(args); //Add the command line arguments to String Buffer }
String str = strb.toString(); //Change to a string so Matcher can use it.
String regex = new String("[a-z]([&][a-z])*");
System.out.println(str); //Test print to ensure the string and regex are
correct
System.out.println(regex);
Pattern p = Pattern.compile(regex, Pattern.CASE_INSENSITIVE |
Pattern.UNICODE_CASE);
Matcher m = p.matcher(regex);
if (m.find()) {
System.out.println("Matched"); }
else {
System.out.println("Not Matched"); }
}
}
Any ideas ? Thanks in advance !