RegEx Help
Why is the code below giving me this error above?
Error:
Exception in thread"AWT-EventQueue-0" java.util.regex.PatternSyntaxException: Unclosed group near index 275
<td bgcolor='#ffffcc' align=center>?([0-9]{2})/([0-9]{2})/([0-9]{4})</td><td bgcolor='#ffffcc' align=center>([^<]\)</td><td bgcolor='#ffffcc' align=center><a href='randomfriend.phtml\?randomfriend=.*?'><b>([^<]\)</b></a></td><td bgcolor='#ffffcc' align=center>([^<]\) NP</td>
RegEx:
String pat ="<td bgcolor='#ffffcc' align=center>?([0-9]{2})/([0-9]{2})/([0-9]{4})</td><td bgcolor='#ffffcc' align=center>([^<]+)</td><td bgcolor='#ffffcc' align=center><a href='randomfriend.phtml\\?randomfriend=.*?'><b>([^<]+)</b></a></td><td bgcolor='#ffffcc' align=center>([^<]+) NP</td>";
[1296 byte] By [
ChromoXNHa] at [2007-11-27 2:24:41]

I would strongly recommend either:
1) write shorter regular expressions, or
2) at least break up the long regexps into something easier to read, for example:
String pat = "<td bgcolor='#ffffcc' align=center>?"
+ "([0-9]{2})/([0-9]{2})/([0-9]{4})"
+ "</td><td bgcolor='#ffffcc' align=center>"
+ "([^<]+)"
+ "</td><td bgcolor='#ffffcc' align=center>"
+ "<a href='randomfriend.phtml\\?randomfriend=.*?'><b>"
+ "([^<]+)"
+ "</b></a></td><td bgcolor='#ffffcc' align=center>"
+ "([^<]+)"
+ " NP</td>";
Note that you could add comments to that, as in:
+ "((?:dog)|(?:cat)|(?:monkey))" // search for dogs, cats, and monkeys
Actually, the quoted closing brace in the error message is missing from the code you quoted. This makes me suspect that you might have changed the code without recompiling.
I wonder also if that question mark after the first > character might not have the effect you think it will.
It's not getting escaped, it's getting mangled. Everywhere the subexpression "([^<]+)" appears in the regex, the '+' is replaced with a '\' before it gets compiled by the Pattern class. I've never heard of that happening to anyone else, so there must be something in the surrounding code that's causing it.