我想使用 Java 语言在页面源中查找 regx 及其出现次数。我试图搜索的值如下面的程序中给出。 标签之间可能有一个或多个空格。我无法为此值形成正则表达式。有人可以帮我找到这个值的正则表达式吗? 我的检查 regx 的程序如下所示-
<小时/>String regx=""<img height=""1"" width=""1"" style=""border-style:none;"" alt="""" src=""//api.adsymptotic.com/api/s/trackconversion?_pid=12170&_psign=3841da8d95cc1dbcf27a696f27ccab0b&_aid=1376&_lbl=RT_LampsPlus_Retargeting_Pixel""/>";
WebDrive driver = new FirefoxDriver();
driver.navigate().to("abc.xom");
int count=0, found=0;
source = driver.getPageSource();
source = source.replaceAll("\\s+", " ").trim();
pattern = Pattern.compile(regx);
matcher = pattern.matcher(source);
while(matcher.find())
{
count++;
found=1;
}
if(found==0)
{
System.out.println("Maximiser not found");
pixelData[rowNumber][2] = String.valueOf(count) ;
pixelData[rowNumber][3] = "Fail";
}
else
{
System.out.println("Maximiser is found" + count);
pixelData[rowNumber][2] = String.valueOf(count) ;
pixelData[rowNumber][3] = "Pass";
}
count=0; found=0;
最佳答案
如果没有原始文本和预期结果,很难说,但你的 Pattern
显然不会按原样编译。
您的代码应该使用单转义双引号 (\"
) 和双转义特殊字符(即 \\?
)和 要编译的 Pattern
。
大致如下:
String regx="<img height=\"1\" width=\"1\" style=\"border-style:none;\" " +
"alt=\"\" src=\"//api.adsymptotic.com/api/s/trackconversion" +
"\\?_pid=12170&_psign=3841da8d95cc1dbcf27a696f27ccab0b" +
"&_aid=1376&_lbl=RT_LampsPlus_Retargeting_Pixel\"/>";
还可以考虑使用适当的框架(即 HTML 的 JSoup)而不是正则表达式来抓取标记。
关于java - 需要帮助在java中形成正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29258495/