需要帮助在 java 中形成正则表达式

Need help to form a regex in java

我想使用语言 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)而不是正则表达式来抓取标记。