Java RegEx从javascript获取其扩展名的变量图像名称

时间:2013-03-09 14:14:57

标签: java regex

我正在尝试从以下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所以请帮助我,让我理解这个方法。  谢谢

3 个答案:

答案 0 :(得分:0)

假设图片始终放在/之后且不包含任何/,您可以使用以下内容:

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

答案 1 :(得分:0)

如果String总是这种形式,我只会这样做:

int startIndex = s.indexOf("rb\\/") + 4;
int endIndex = s.indexOf('\'', startIndex);
String image = s.substring(startIndex, endIndex);

答案 2 :(得分:0)

我会使用以下正则表达式,它应该足够快:

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

这是一个更复杂的例子,假设格式会改变,它主要是为合法的窗口文件名设计的:

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

您可以在这里找到demo