我有一个元素:
<p align="center"><font class="bodytext"><strong><u>Address</u></strong><br /> 123 Upper East , text text2 s (s)465561 </font></p>
或者这种格式可能有点不同:
<p align="center"><font class="bodytext"><strong><u>Address</u></strong><br /> 123 Upper East , text text2 s 465561 </font></p>
我需要的是最后一个数字(只有数字:
465561
最好的方法是什么?我正在考虑使用一些元素提取或正则表达式,但无法弄清楚。
getElementsByTag("font");
和
Pattern titleFinder = Pattern.compile("<a[^>]*>(.*?)</a>", Pattern.DOTALL | Pattern.CASE_INSENSITIVE);
Matcher regexMatcher = titleFinder.matcher(subjectString);
不适合我。
答案 0 :(得分:1)
抓住该项目,例如通过 getElementsByTagName(“font”); (你会得到一个数组)
假设getElementsByTagName(“font”)[0]的内容为“123 Upper East,text text2 s 465561”;
var tmp = document.getElementsByTagName("font")[0].innerText.split(" ");
//将导致数组
[“地址123”,“上部”,“东部”,“,”,“文字”,“文字2”,“s”,“(s)465561”]
var resultnum = tmp[tmp.length-1];
document.getElementById("result").innerHTML="Number is " +resultnum;
//resultnum = resultnum.replace(/(s)/);`
//替换可能(不太确定正则表达式)
答案 1 :(得分:0)
试试这个:
Pattern titleFinder = Pattern.compile(".*\\D+(\\d+)\\D*", Pattern.DOTALL | Pattern.CASE_INSENSITIVE);
答案 2 :(得分:0)
TransformerFactory transFactory = TransformerFactory.newInstance();
Transformer transformer = transFactory.newTransformer();
StringWriter buffer = new StringWriter();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
transformer.transform(new DOMSource(element), new StreamResult(buffer));
String input = buffer.toString();
String fontText = input.substring(input.indexOf("<font>") + 2 + "font".length(), input.indexOf("</font>"));
String textPieces[] = fontText.split(" ");
// textPieces[textPieces.length - 1] will have the last word
答案 3 :(得分:0)
这是强力表达,但它按预期工作,并且非常容易理解 并且你不关心标签,你的字符串中的另一个数字只要与6位数字模式不同
//Get any last 6 digits
String pattern = "(.*)([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])(.*)";
Pattern p = Pattern.compile(pattern);
String updated = "<p align=\"center\"><font class=\"bodytext\"><strong><u>Address</u></strong><br /> 123 Upper East , text text2 s (s)465561 </font></p>";
Matcher m = p.matcher(updated);
if(m.matches()){
System.out.println(m.group(2)+m.group(3)+m.group(4)+m.group(5)+m.group(6)+m.group(7));
}
GOT:
465561
Process finished with exit code 0
m.group(1)和最后一组m.group(7)是剩饭剩菜:)