3

我有一个元素:

 <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);

不要真的为我工作。

4

4 回答 4

1

抓取项目,例如通过getElementsByTagName("font");(你会得到一个数组)
假设 getElementsByTagName("font")[0] 的内容是“ 123 Upper East , text text2 s 465561 ”;

var tmp = document.getElementsByTagName("font")[0].innerText.split(" ");

// 将产生数组
["Address 123", "Upper", "East", ",", "text", "text2", "s", "(s)465561"]

var resultnum = tmp[tmp.length-1];  
document.getElementById("result").innerHTML="Number is " +resultnum;

//resultnum = resultnum.replace(/(s)/);`

//替换可能的(s)(不太确定正则表达式)

于 2013-05-30T17:03:39.330 回答
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
于 2013-05-30T17:00:42.200 回答
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));

    }

得到:

465561

Process finished with exit code 0

m.group(1) 和最后一个 m.group(7) 是剩菜:)

于 2013-05-30T17:28:52.977 回答
0

试试这个:

Pattern titleFinder = Pattern.compile(".*\\D+(\\d+)\\D*", Pattern.DOTALL | Pattern.CASE_INSENSITIVE);
于 2013-05-30T16:58:28.087 回答