Get URL of images in CSS file using Java?

I am trying to get the URLs for images (all MIME types) in a remote CSS file using Java.

I am using jsoup to get the css url.

After countless hours viewing CSS Parser, I could not figure it out due to lack of documentation.

I also looked at some other steps, but confused me even more:

I also saw some examples using regex, but I don't know very well how to implement it in java.

Anyone have any suggestions on how to go about this issue?

+7
source share
2 answers

In Java, you need to use Pattern and Matcher from java.util.regex .

You compile your template, then you create an instance of your template with your string, and then you look for everything that matches your template.

 Pattern p = Pattern.compile("..."); Matcher m = p.matcher("your CSS file as a String"); while (m.find()) { // Here use m.group(), m.group(1), ... } 

The CSS 2.1 specification states:

The format of the URI value is "url (", followed by optional free space, followed by the optional single quote character () or double quote ("), followed by the URI itself, and then the optional single quote ('), or the double character quotation marks (") followed by an extra space followed by ')'. Both quotation marks must be the same.

So you can use a regex like this:

 url\(\s*(['"]?+)(.*?)\1\s*\) 

.*? Not greedy, allowing you to accept as few characters as possible. A possessive quantifier avoids any indentation in ['"]?+ .

+6
source

You can also use ph-css for this. See the “Visit All URLs Contained in CSS” example located at https://github.com/phax/ph-css#code-examples . I can not make it much easier :)

0
source

All Articles