Reputation: 57
Given the String:
<td>4</td><td>punz of damage</td><td><img src='images/no.png'></img></td><td>May 26, 2015 10:28:12 PM</td><td>30</td><td>Nov 26, 2017 10:28:12 PM</td>
I would like to be able to return only the value between the second element.
How would I accomplish this? I have the following so far:
private static Pattern p = Pattern.compile("<td>(.+?)</td>");
public static String getName(String in) {
Matcher m = p.matcher(in);
if (m.matches()) {
return m.group(1);
} else {
return null;
}
}
Upvotes: 1
Views: 1173
Reputation: 784958
Use matcher.find()
in a loop instead of matches
and keep a counter:
private static Pattern p = Pattern.compile("<td>(.+?)</td>");
public static String getName(String in) {
Matcher m = p.matcher(in);
for (i=0; i<1 && m.find(); i++);
if (i==0) {
return null;
} else {
return m.group(1);
}
}
Caution: Parsing HTML/XML using regex can be error prone.
Upvotes: 1