如何更改以下代码,以便它不关心大小写?
public static String tagValue(String inHTML, String tag)
throws DataNotFoundException {
String value = null;
Matcher m = null;
int count = 0;
try {
String searchFor = "<" + tag + ">(.*?)</" + tag + ">";
Pattern pattern = Pattern.compile(searchFor);
m = pattern.matcher(inHTML);
while (m.find()) {
count++;
return inHTML.substring(m.start(), m.end());
// System.out.println(inHTML.substring(m.start(), m.end()));
}
} catch (Exception e) {
throw new DataNotFoundException("Can't Find " + tag + "Tag.");
}
if (count == 0) {
throw new DataNotFoundException("Can't Find " + tag + "Tag.");
}
return inHTML.substring(m.start(), m.end());
}
Run Code Online (Sandbox Code Playgroud)
给Pattern.CASE_INSENSITIVE国旗Pattern.compile:
String searchFor = "<" + tag + ">(.*?)</" + tag + ">";
Pattern pattern = Pattern.compile(searchFor, Pattern.CASE_INSENSITIVE);
m = pattern.matcher(inHTML);
Run Code Online (Sandbox Code Playgroud)
(哦,考虑解析XML/HTML 而不是使用正则表达式来匹配非正规语言.)
| 归档时间: |
|
| 查看次数: |
263 次 |
| 最近记录: |