Re: regex bug jre6???
hiwa,
Thanks for taking the time to write that up and respond. It helped me
shed a little more light on the issue. It's not the "\\" that causes
the problem, but rather "\\Q". I've modified RegX and regex.txt a bit
to highlight the problem. Runtime output is from 1.4, 1.5, and 1.6
(with the Exception pasted in).
/** content of regex.txt **
^((VINCENT)|(GEORGIA)|(GIACOMO\\QUARENGHI)|(CLAUDE))$[newline]
***************************/
import java.io.*;
import java.util.regex.*;
public class RegX
{
public static void main(String[] args)
{
System.out.print("\nJava Version " +
System.getProperty("java.specification.version"));
System.out.println("\n----------------");
String regex = null;
String text = "GEORGIA";
try
{
BufferedReader br
= new BufferedReader(new FileReader("regex.txt"));
regex = br.readLine();
}
catch (Exception e)
{
e.printStackTrace();
}
Pattern pat = Pattern.compile(regex);
Matcher mat = pat.matcher(text);
System.out.println("\nLooking for \"" + text + "\" in \"" +
regex + "\"");
while (mat.find())
{
System.out.println("\t--> " + mat.group());
}
System.out.print("\n\"" + text + "\" matches \"" +
regex + "\"... ");
System.out.println(text.matches(regex));
System.out.println
("\n====================================================\n");
}
}
OUTPUT...
Java Version 1.4
----------------
Looking for "GEORGIA" in
"^((VINCENT)|(GEORGIA)|(GIACOMO\\QUARENGHI)|(CLAUDE))$"
--> GEORGIA
"GEORGIA" matches
"^((VINCENT)|(GEORGIA)|(GIACOMO\\QUARENGHI)|(CLAUDE))$"... true
====================================================
Java Version 1.5
----------------
Looking for "GEORGIA" in
"^((VINCENT)|(GEORGIA)|(GIACOMO\\QUARENGHI)|(CLAUDE))$"
--> GEORGIA
"GEORGIA" matches
"^((VINCENT)|(GEORGIA)|(GIACOMO\\QUARENGHI)|(CLAUDE))$"... true
====================================================
Java Version 1.6
----------------
Exception in thread "main" java.util.regex.PatternSyntaxException:
Illegal/unsupported escape squence near index 31
^((VINCENT)|(GEORGIA)|(GIACOMO\\QUARENGHI)|(CLAUDE))$
^
at java.util.regex.Pattern.error(Unknown Source)
at java.util.regex.Pattern.escape(Unknown Source)
at java.util.regex.Pattern.atom(Unknown Source)
at java.util.regex.Pattern.sequence(Unknown Source)
at java.util.regex.Pattern.expr(Unknown Source)
at java.util.regex.Pattern.group0(Unknown Source)
at java.util.regex.Pattern.sequence(Unknown Source)
at java.util.regex.Pattern.expr(Unknown Source)
at java.util.regex.Pattern.group0(Unknown Source)
at java.util.regex.Pattern.sequence(Unknown Source)
at java.util.regex.Pattern.expr(Unknown Source)
at java.util.regex.Pattern.compile(Unknown Source)
at java.util.regex.Pattern.<init>(Unknown Source)
at java.util.regex.Pattern.compile(Unknown Source)
at RegX.main(RegX.java:25)