我正在编写一个程序,需要检查一组字符的存在位置。我的代码目前是:
String checkerLoop = "ForeclosureResutls_CaseNum_";
Pattern checkerLoopPattern = Pattern.compile("(?<="+Pattern.quote(checkerLoop)+").*?(?="+checkerNumber+")");
Matcher checkerLoopMatcher = checkerLoopPattern.matcher(scraper.getPage().getWebResponse().getContentAsString());
while (checkerLoopMatcher.find()) {
checker = true;
}
我需要查找的句子是“ForeclosureResutls_CaseNum _”+ checkerNumber,其中checker number是一个int。我尝试根据以前的代码编写这段代码,以便在两组之间找到一组字符,所以我相信这可能就是为什么这段代码不能正常工作。
示例输入字符串如下:
<a id="SheetContentPlaceHolder_ctl00_gvForeclosureResutls_lbCaseNum_0" href="javascript:__doPostBack('ctl00$SheetContentPlaceHolder$ctl00$gvForeclosureResutls$ctl02$lbCaseNum','')" style="display:inline-block;width:100px;">CV-13-798497</a>
</td><td align="center">488-05-029</td><td align="center">I</td><td align="center">01/02/2013</td>
</tr><tr style="background-color:Gainsboro;">
<td align="left">UNKNOWN HEIRS, ETC OF D.C. RUFUS, ET AL </td><td align="left">10603 HAMPDEN AVENUE</td><td align="center">CLEVELAND</td><td align="center">44108-0000</td><td align="center">
<a id="SheetContentPlaceHolder_ctl00_gvForeclosureResutls_lbCaseNum_1" href="javascript:__doPostBack('ctl00$SheetContentPlaceHolder$ctl00$gvForeclosureResutls$ctl03$lbCaseNum','')" style="display:inline-block;width:100px;">CV-13-798498</a>
</td><td align="center">109-16-094</td><td align="center">A</td><td align="center">01/02/2013</td>
</tr><tr style="background-color:LightGrey;">
<td align="left">SHARECE MILLER, ET AL </td><td align="left">13514 ALVIN AVENUE</td><td align="center">GARFIELD HTS</td><td align="center">44105-0000</td><td align="center">
<a id="SheetContentPlaceHolder_ctl00_gvForeclosureResutls_lbCaseNum_2" href="javascript:__doPostBack('ctl00$Shee
答案 0 :(得分:1)
好的,这就是我所拥有的。我并没有完全满足你的要求,但这应该会让你走上正确的道路。
首先,在此演示数据中找不到ForeclosureResutls_CaseNum_
。 ForeclosureResutls_lbCaseNum
是,所以我就是这样。
另外,我忽略了checkerNumber
并假设您要检查任何号码,因为此输入中有三个,我不知道你的派生方式如何。因此\\d
。
根据我的理解,你在帖子中使用的正则表达式是疯狂的,根据你需要做的事情。相比之下,我使用的那个是微不足道的。
试试这个:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
<P>{@code java ParseForclosureResultsXmpl}</P>
**/
public class ParseForclosureResultsXmpl {
public static final void main(String[] igno_red) {
String sLS = System.getProperty("line.separator", "\n");
StringBuilder sdInput = new StringBuilder().
append("<a id=\"SheetContentPlaceHolder_ctl00_gvForeclosureResutls_lbCaseNum_0\" href=\"javascript:__doPostBack('ctl00$SheetContentPlaceHolder$ctl00$gvForeclosureResutls$ctl02$lbCaseNum','')\" style=\"display:inline-block;width:100px;\">CV-13-798497</a>").append(sLS).
append(" </td><td align=\"center\">488-05-029</td><td align=\"center\">I</td><td align=\"center\">01/02/2013</td>").append(sLS).
append(" </tr><tr style=\"background-color:Gainsboro;\">").append(sLS).
append(" <td align=\"left\">UNKNOWN HEIRS, ETC OF D.C. RUFUS, ET AL </td><td align=\"left\">10603 HAMPDEN AVENUE</td><td align=\"center\">CLEVELAND</td><td align=\"center\">44108-0000</td><td align=\"center\">").append(sLS).
append(" <a id=\"SheetContentPlaceHolder_ctl00_gvForeclosureResutls_lbCaseNum_1\" href=\"javascript:__doPostBack('ctl00$SheetContentPlaceHolder$ctl00$gvForeclosureResutls$ctl03$lbCaseNum','')\" style=\"display:inline-block;width:100px;\">CV-13-798498</a>").append(sLS).
append(" </td><td align=\"center\">109-16-094</td><td align=\"center\">A</td><td align=\"center\">01/02/2013</td>").append(sLS).
append(" </tr><tr style=\"background-color:LightGrey;\">").append(sLS).
append(" <td align=\"left\">SHARECE MILLER, ET AL </td><td align=\"left\">13514 ALVIN AVENUE</td><td align=\"center\">GARFIELD HTS</td><td align=\"center\">44105-0000</td><td align=\"center\">").append(sLS).
append(" <a id=\"SheetContentPlaceHolder_ctl00_gvForeclosureResutls_lbCaseNum_2\" href=\"javascript:__doPostBack('ctl00$Shee").append(sLS);
String sRqdValuePrefix = "ForeclosureResutls_lbCaseNum_";
Pattern checkerLoopPattern = Pattern.compile(sRqdValuePrefix + "\\d");
Matcher m = checkerLoopPattern.matcher(""); //Unused. so the matcher can be reused in the loop.
int iLn = 0;
String[] asInput = sdInput.toString().split(sLS);
for(String s : asInput) {
iLn++; //1st iteration: Was zero, now 1
//Resuing matcher instead of retrieving new one from Pattern each iteration
m.reset(s);
if(m.find()) {
int iCheckerNumber = Integer.parseInt(s.substring(m.start() + sRqdValuePrefix.length(), m.end()));
System.out.println("Found on line " + iLn + ", at index " + m.start() + " with checker number " + iCheckerNumber);
}
}
}
}
输出:
[C:\java_code\]java ParseForclosureResultsXmpl
Found on line 1, at index 39 with checker number 0
Found on line 5, at index 57 with checker number 1
Found on line 9, at index 57 with checker number 2
提出任何问题。