使用Java获取CSS文件中的图像的URL? [英] Get URL of images in CSS file using Java?

查看:437
本文介绍了使用Java获取CSS文件中的图像的URL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用Java获取远程CSS文件中图片(所有MIME类型)的网址。



我使用jsoup获取网址在查看 CSS解析器无数小时后,我无法看到



我还看了一些其他胎面,但更让我困惑了:





我也看过一些使用regex的例子,我不太熟悉如何在java中实现它。



有人会有一些建议如何去解决这个问题吗?

解决方案

在Java中,你必须使用 Pattern Matcher java.util.regex 包。



编译模式,然后使用字符串实例化匹配器,查找符合您的模式的所有内容。

 模式p = Pattern.compile(...); 
Matcher m = p.matcher(your CSS file as a String);
while(m.find()){
//这里使用m.group(),m.group(1),...
}

CSS 2.1规范规定:


URI值的格式为'url('后跟可选的单引号(')或双引号()字符后面跟随URI本身,后跟可选的单引号(')或双引号)字符后跟可选的空格,后跟')'。


因此,您可以使用像这样的正则表达式:

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

。*?占有率尽可能少的字符。占有量词避免了 [']?+ 中的任何回溯。


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

I am using jsoup to get the URL of the css.

After countless hours of looking at CSS Parser I couldn't figure it out due to the lack of documentation.

I also looked at some other treads, but have just confused me even more:

I've also seen some examples using regex, but I am not too familiar how to implement it in java.

Would anyone have some suggestions on how to go at this problem?

解决方案

In Java, you have to use a Pattern and a Matcher from the java.util.regex package.

You compile your pattern, then you instantiate your matcher with your string and then you look for everything that matches your pattern.

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 spec states:

The format of a URI value is 'url(' followed by optional white space followed by an optional single quote (') or double quote (") character followed by the URI itself, followed by an optional single quote (') or double quote (") character followed by optional white space followed by ')'. The two quote characters must be the same.

Thus you could use a regex like this one:

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

The .*? is non-greedy allowing you to take as few characters as necessary. The possessive quantifier avoids any backtrack in ['"]?+.

这篇关于使用Java获取CSS文件中的图像的URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆