For Testing
For Testing

Reputation: 421

Need help to form a regex in java

I want to find a regx and occurrences of it in the page source using language Java. The value I am trying to search is as given in the program below. There might be one or more spaces between tags. I am not able to form a regx for this value. Can some one please help me to find the regx for this value? My program which checks regx is as given below-


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;

Upvotes: 1

Views: 87

Answers (1)

Mena
Mena

Reputation: 48444

Hard to tell without the original text and expected result, but your Pattern clearly won't compile as is.

You should single-escape double quotes (\") and double-escape special characters (i.e. \\?) for your code and your Pattern to compile.

Something in the lines of:

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\"/>";

Also consider scraping markup with appropriate framework (i.e. JSoup for HTML) instead of regex.

Upvotes: 1

Related Questions