更新时间:2022-12-29 10:08:14
您的两个主要选项是:
1)首选但可能很复杂:使用XML / HTML解析器并在第一个a元素中获取文本。例如使用 Jsoup (感谢@ alpha123):
1) preferred but potentially complicated: using an XML/HTML parser and getting the text within the first "a" element. e.g. using Jsoup (thanks @alpha123):
Jsoup.parse("<a>3</a>").select("a").first().text(); // => "3"
2)更容易但不太可靠:使用常规表达式,用于提取< a>
和< / a>
字符串之间的字符。例如:
2) easier but not very reliable: using a regular expression to extract the characters between the <a>
and </a>
strings. e.g.:
String s = "<a>3</a>";
Pattern p = Pattern.compile("<a>(.*?)</a>")
Matcher m = p.matcher(s);
if (m.find()) {
System.out.println(m.group(1)); // => "3"
}