0

Here is a string:

"http://l2.yimg.com/bt/api/res/1.2/iis49xBsStLiYI6LjauR6Q--/YXBwaWQ9eW5ld3M7Zmk9ZmlsbDtoPTg2O3E9NzU7dz0xMzA-/http://media.zenfs.com/fr_FR/News/LeMonde.fr/1515504_3_f73c_le-cyber-harcelement-est-une-realite-trop-lo_450282425a88c544c2ff4121a5d9dab4.jpg"

This string is a concatenation of two URLs. I would like to extract only the second URL:

"http://media.zenfs.com/fr_FR/News/LeMonde.fr/1515504_3_f73c_le-cyber-harcelement-est-une-realite-trop-lo_450282425a88c544c2ff4121a5d9dab4.jpg"

How can I do that using Java?

2
  • 1
    A simple str.substring(str.indexOf("http://", 1)) is enough. Commented Feb 5, 2014 at 13:14
  • I like str.substring(108). It does exactly what the poster asked for. Commented Feb 5, 2014 at 15:20

4 Answers 4

2

Remove everything up to "http://" not found at the start:

String url2 = str.replaceAll("(?i).+(?=https?://)", "");

This will work case insensitively and match http or https protocols.

Sign up to request clarification or add additional context in comments.

6 Comments

+1 for handling https, although I hardly believe case insensitivity is necessary.
thanks, but I'm working pro bono atm - capped out hours ago :)
+1; I didn't know that you can use (?<flags>) in java.
@RyanCarlson afaik (?ims) are supported. global is on by default.
It doesn't say anywhere on Pattern's summary, but I did find another interesting thing: in the regex (abc((?i)xyz), xyz is case insensitive, but abc isn't.
|
2

Try this. "str" is the url string

System.out.println(str.substring(str.lastIndexOf("http:")));

Comments

2

If you want to extract the URL, just find the last instance of http, and take the substring:

String secondUrl = firstUrl.substring(firstUrl.lastIndexOf("http"));

Comments

1

Try using string's .split() method, like this:

String oneURL = twoURLs.split("(?<!^)(?=http://)")[1];

This splits the string in places that are not at the end of the string, but are followed by http://. With that, you should end up with an array like this:

["http://l2.yimg.com/bt/api/res/1.2/iis49xBsStLiYI6LjauR6Q--/YXBwaWQ9eW5ld3M7Zmk9ZmlsbDtoPTg2O3E9NzU7dz0xMzA-/", "http://media.zenfs.com/fr_FR/News/LeMonde.fr/1515504_3_f73c_le-cyber-harcelement-est-une-realite-trop-lo_450282425a88c544c2ff4121a5d9dab4.jpg"]

[1] takes only the second element of that array.

Explanation and demonstration of the regex here: http://regex101.com/r/eW6mZ0

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.