how to check why a regular expression is not working?
This regular expression is not finding any matches. In other languages I run it in, it works great. In online regular expression testers like rubular.com it works great.
In java, it does not work.
What is different about java regular expressions that they break things like this?
Why doesn't it work? Furthermore, how can I 开发者_开发问答figure out in the future why they wont match?
sample data
<link>http://www.wunderground.com/US/MI/Milan.html</link>
<description><![CDATA[Temperature: 89.4°F | Humidity: 62% | Pressure: 29.65in (Steady) | Conditions: Clear | Wind Direction: NW | Wind Speed: 1.6mph<img src="http://server.as5000.com/AS5000/adserver/image?ID=WUND-00070&C=0" width="0" height="0" border="0"/>]]>
</description>
and the code
protected void parseTemperature()
{
Pattern p = Pattern.compile("/Temperature: ([0-9.]+)°/");
Matcher m = p.matcher(this.xml);
if (m.find())
{
this.temperature = Double.valueOf(m.group(1));
}
}
Remove the /
characters surrounding the regex.
Pattern.compile
takes a raw expression, not wrapped in /
s.
In Java you do not want to put delimiters on the ends of your expression. Change it to:
Pattern p = Pattern.compile("Temperature: ([0-9.]+)°");
(Forward slashes removed)
You need to remove the /
characters. The regular expression engine in Java will try to match those.
Remove the '/' at the beginning and end of your pattern, and it works just fine for me.
精彩评论