Reputation: 267390
My HTML looks like:
<td class="price" valign="top"><font color= "blue"> $ 5.93 </font></td>
I tried:
String result = "";
Pattern p = Pattern.compile("\"blue\"> $ (.*) </font></td>");
Matcher m = p.matcher(text);
if(m.find())
result = m.group(1).trim();
Doesn't seem to be matching.
Am I missing an escape character?
Upvotes: 1
Views: 646
Reputation: 719739
Unless escaped at the regex level, $
means match the end of line. And to get the single \
needed to escape the $
it needs to be escaped in the String literal; i.e. two \
characters. So ...
... Pattern.compile("\"blue\"> \\$ (.*) </font></td>");
But the folks who commented that you shouldn't use regexes to parse HTML are absolutely right!! Unless you want chronically fragile code, your code should use a strict or non-strict HTML parser.
Upvotes: 2