0

我有

String content= "<a data-hovercard=\"/ajax/hovercard/group.php?id=180552688740185\">
                 <a data-hovercard=\"/ajax/hovercard/group.php?id=21392174\">"

我想得到和之间的所有"group.php?id=" id "\""

例如:180552688740185

这是我的代码:

String content1 = "";
Pattern script1 = Pattern.compile("group.php?id=.*?\"");
Matcher mscript1 = script1.matcher(content);
while (mscript1.find()) {
    content1 += mscript1.group() + "\n";
}

但由于某种原因,它不起作用。

你能给我一些建议吗?

4

1 回答 1

2

你为什么用它.*?来匹配id. .*?将匹配每个字符。您只需要检查digits. 所以,只需使用\\d.

此外,您需要捕获id然后打印它。

// To consider special characters as literals
String str = Pattern.quote("group.php?id=") + "(\\d*)";

Pattern script1 = Pattern.compile(str);
// Your matcher line
while (mscript1.find()) {
    content += mscript1.group(1) + "\n";   // Capture group 1 contains your id
}
于 2012-10-15T14:55:42.777 回答