我正试图从以下javascript中获取图像名称。
var g_prefetch ={'Im': {url:'/az/hprichbg/rb/WhiteTippedRose_ROW10477559674_1366x768.jpg', hash:'674'}
问题:
图像的名称是可变的。也就是说,在上面的示例代码中,图像有规律地变化
我想要的输出:
WhiteTippedRose_ROW10477559674_1366x768.jpg
我尝试了以下regExp:
Pattern p = Pattern.compile("{'Im': {url:'\/az\/hprichbg\/rb\/(.*?).jpg', hash:'674'}");
//System.out.println(p);
Matcher m=p.matcher(out);
if(m.find()) {
System.out.println(m.group());
}
我不知道太多RegExp,所以请帮助我,让我了解方法。谢谢
我会使用以下正则表达式,它应该足够快:
Pattern p = Pattern.compile("[^/]+\.jpg");
Matcher m = p.matcher(str);
if (m.find()) {
String match = m.group();
System.out.println(match);
}
这将匹配以.jpg结尾的完整字符序列,不包括/。
我认为正确的方法是检查文件名的正确合法性。
以下是Windows的非法字符列表:"\/:*?"<>|"
适用于Mac /:
Linux/Unix /
;
这里有一个更复杂的例子,假设格式会改变,它主要是为合法的Window文件名设计的:
String s = "{'Im': {url:'\/az\/hprichbg\/rb\/?*<>WhiteTippedRose_ROW10477559674_1366x768.jpg', hash:'674'}";
Pattern p = Pattern.compile("[^\/:*?"<>|]+\.jpg");
Matcher m = p.matcher(s);
if (m.find()) {
String match = m.group();
System.out.println(match);
}
这仍然会打印WhiteTippedRose_ROW10477559674_1366x768.jpg
在这里您可以找到一个演示
假设图像总是放在/
之后,并且不包含任何/
,则可以使用以下内容:
String s = "{'Im': {url:'\/az\/hprichbg\/rb\/WhiteTippedRose_ROW10477559674_1366x768.jpg', hash:'674'}";
s = s.replaceAll(".*?([^/]*?\.jpg).*", "$1");
System.out.println("s = " + s);
输出:
s=WhiteTippedRose_ROW10477559674_1366x768.jpg
实质:
.*? skip the beginning of the string until the next pattern is found
([^/]*?\.jpg) a group like "xxx.jpg" where xxx does not contain any "/"
.* rest of the string
$1 returns the content of the group
如果字符串总是这种形式,我只需执行:
int startIndex = s.indexOf("rb\/") + 4;
int endIndex = s.indexOf(''', startIndex);
String image = s.substring(startIndex, endIndex);