0

I am trying to get the image name from the following javascript.

var g_prefetch ={'Im': {url:'\/az\/hprichbg\/rb\/WhiteTippedRose_ROW10477559674_1366x768.jpg', hash:'674'}

Problem:

The name of the image is variable. That is, in the above example code the image changes regularly.

Output I want:

WhiteTippedRose_ROW10477559674_1366x768.jpg

and i tried the following 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());

                            }

I don't know too much RegExp so please help me and let me understand the approach. Thank You

3 Answers3

0

Assuming that the image is always placed after a / and does not contain any /, you can use the following:

String s = "{'Im': {url:'\\/az\\/hprichbg\\/rb\\/WhiteTippedRose_ROW10477559674_1366x768.jpg', hash:'674'}";
s = s.replaceAll(".*?([^/]*?\\.jpg).*", "$1");
System.out.println("s = " + s);

outputs:

s = WhiteTippedRose_ROW10477559674_1366x768.jpg

In substance:

.*?             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
assylias
  • 321,522
  • 82
  • 660
  • 783
0

If the String is always of this form, I would simply do:

int startIndex = s.indexOf("rb\\/") + 4;
int endIndex = s.indexOf('\'', startIndex);
String image = s.substring(startIndex, endIndex);
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
0

I would use the following regex, it should be fast enough:

Pattern p = Pattern.compile("[^/]+\\.jpg");
Matcher m = p.matcher(str);
if (m.find()) {
  String match = m.group();
  System.out.println(match);
}

This will match the a full sequence of characters ending with .jpg not including /.

I think that the correct approach will be to check the correct legality of a file name.

Here is a list of not legal characters for Windows: "\\/:*?\"<>|" for Mac /: Linux/Unix /;

Here is more complex example assuming format will change , it is mostly designed for legal Window file name:

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);
}

This will still print WhiteTippedRose_ROW10477559674_1366x768.jpg

Here you may find a demo

Michael
  • 2,827
  • 4
  • 30
  • 47