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]
# 1
Because you have an unclosed group.I can't help noticing this part:<b>([^<]\)</b>in which you quote the closing brace.
paulcwa at 2007-7-12 2:32:01 > top of Java-index,Java Essentials,Java Programming...
# 2

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.

paulcwa at 2007-7-12 2:32:01 > top of Java-index,Java Essentials,Java Programming...
# 3
I don't get it though, where ever there is a + it is replaced with a \ therefore escaping my ) that ends the group... Why?
ChromoXNHa at 2007-7-12 2:32:01 > top of Java-index,Java Essentials,Java Programming...
# 4
> I don't get it though, where ever there is a + it is> replaced with a \ therefore escaping my ) that ends> the group... Why?can you restate that?
mkoryaka at 2007-7-12 2:32:01 > top of Java-index,Java Essentials,Java Programming...
# 5
Look at my code... Wherever a plus sign(+) is... It automatically gets escaped as you see in the error, meaning that a + turns into a \ therefore escaping my ) which closes the group.
ChromoXNHa at 2007-7-12 2:32:01 > top of Java-index,Java Essentials,Java Programming...
# 6
i dont understand what you mean by "automatically gets escaped as you see in the error" during run time it gets escaped?
mkoryaka at 2007-7-12 2:32:01 > top of Java-index,Java Essentials,Java Programming...
# 7

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.

uncle_alicea at 2007-7-12 2:32:01 > top of Java-index,Java Essentials,Java Programming...
# 8
What do you do with pat? Anything other than pass it to compile()?
paulcwa at 2007-7-12 2:32:01 > top of Java-index,Java Essentials,Java Programming...