5

I have the following string:

http://xxx/Content/SiteFiles/30/32531a5d-b0b1-4a8b-9029-b48f0eb40a34/05%20%20LEISURE.mp3?&mydownloads=true

How can I extract the part after 30/? In this case, it's 32531a5d-b0b1-4a8b-9029-b48f0eb40a34.I have another strings having same part upto 30/ and after that every string having different id upto next / which I want.

user1196969
  • 335
  • 2
  • 5
  • 14
  • 1
    Given an arbitrary string, what are the rules for determining which part you keep? Do you only keep the part that matches the GUID format that is [specified in .NET with a D](http://msdn.microsoft.com/en-us/library/97af8hh4.aspx)? – Adam Mihalcin Jun 06 '12 at 04:52
  • if there are a lot of such strings with different prefixes and as you say a pattern, you might wanna look at `Regular Expressions` – Ankit Jun 06 '12 at 04:58
  • It would be better if you do sothing similar to this http://stackoverflow.com/questions/27745/getting-parts-of-a-url-regex – dilip kumbham Jun 06 '12 at 05:23

4 Answers4

6

You can do like this:

String s = "http://xxx/Content/SiteFiles/30/32531a5d-b0b1-4a8b-9029-b48f0eb40a34/05%20%20LEISURE.mp3?&mydownloads=true";
        System.out.println(s.substring(s.indexOf("30/")+3, s.length()));
UVM
  • 9,776
  • 6
  • 41
  • 66
  • While this is certainly an answer to his question, I don't think he wanted the entire rest of the string. Plus, why would you use the 2-argument version of substring, if you're just going to use length as the 2nd argument? Look up the 1-arg version. – billjamesdev Jun 06 '12 at 05:14
4

split function of String class won't help you in this case, because it discards the delimiter and that's not what we want here. you need to make a pattern that looks behind. The look behind synatax is:

(?<=X)Y

Which identifies any Y that is preceded by a X.

So in you case you need this pattern:

(?<=30/).*

compile the pattern, match it with your input, find the match, and catch it:

String input = "http://xxx/Content/SiteFiles/30/32531a5d-b0b1-4a8b-9029-b48f0eb40a34/05%20%20LEISURE.mp3?&mydownloads=true";
Matcher matcher = Pattern.compile("(?<=30/).*").matcher(input);
matcher.find();
System.out.println(matcher.group());
Untitled
  • 781
  • 1
  • 6
  • 24
2

Just for this one, or do you want a generic way to do it ?

String[] out = mystring.split("/")
return out[out.length - 2]

I think the / is definitely the delimiter you are searching for. I can't see the problem you are talking about Alex

EDIT : Ok, Python got me with indexes.

jlengrand
  • 12,152
  • 14
  • 57
  • 87
1

Regular expression is the answer I think. However, how the expression is written depends on the data (url) format you want to process. Like this one:

    Pattern pat = Pattern.compile("/Content/SiteFiles/30/([a-z0-9\\-]+)/.*");
    Matcher m = pat.matcher("http://xxx/Content/SiteFiles/30/32531a5d-b0b1-4a8b-9029-b48f0eb40a34/05%20%20LEISURE.mp3?&mydownloads=true");
    if (m.find()) {
        System.out.println(m.group(1));
    }
xiaofeng.li
  • 8,237
  • 2
  • 23
  • 30