20

I want to parse the url from a String in android. The example String is

"This is a new message. The content of the message is in 'http://www.example.com/asd/abc' "

I want to parse the url http://www.example.com/asd/abc from the String without using the subString method.

2
  • It will be a RegExp then - do always find the url inside the single quote char or do yo need a matcher for urls in general? Commented Jun 17, 2011 at 10:13
  • Try with the method that I have posted Commented Jun 17, 2011 at 10:35

7 Answers 7

45

Use this:

public static String[] extractLinks(String text) {
    List<String> links = new ArrayList<String>();
    Matcher m = Patterns.WEB_URL.matcher(text);
    while (m.find()) {
        String url = m.group();
        Log.d(TAG, "URL extracted: " + url);
        links.add(url);
    }

    return links.toArray(new String[links.size()]);
}
Sign up to request clarification or add additional context in comments.

Comments

33

Updated:

You can use regular expression with Patterns.WEB_URL regular expression to find all urls in your text.


Original:

You can use Uri.parse(String uriString) function.

Comments

11

If you are parsing the links for the purpose of styling them, Linkify is an elegant solution:

descriptionTextView.setText("This text contains a http://www.url.com")
Linkify.addLinks(descriptionTextView, Linkify.WEB_URLS);

You can also change the default colour of the link:

descriptionTextView.setLinkTextColor(ContextCompat.getColor(getContext(),
                R.color.colorSecondary));

The result looks like this:

enter image description here

Comments

6

Yes its possible. Try with the following code sample

ArrayList retrieveLinks(String text) {
        ArrayList links = new ArrayList();

        String regex = "\\(?\\b(http://|www[.])[-A-Za-z0-9+&@#/%?=~_()|!:,.;]*[-A-Za-z0-9+&@#/%=~_()|]";
        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(text);
        while(m.find()) {
        String urlStr = m.group();
        char[] stringArray1 = urlStr.toCharArray();

        if (urlStr.startsWith("(") && urlStr.endsWith(")"))
        {

            char[] stringArray = urlStr.toCharArray(); 

            char[] newArray = new char[stringArray.length-2];
            System.arraycopy(stringArray, 1, newArray, 0, stringArray.length-2);
            urlStr = new String(newArray);
            System.out.println("Finally Url ="+newArray.toString());

        }
        System.out.println("...Url..."+urlStr);
        links.add(urlStr);
        }
        return links;
        }

Thanks Deepak

Comments

5

I use this in Kotlin

fun findAllUrls(text: String): List<String> {
    return Regex(Patterns.WEB_URL.pattern()).findAll(text).map {
        it.value
    }.toList()
}

Comments

0

You can just create a new URL passing the string as the argument to the constructor. Then you can use the various methods on the URL class to access the different parts of the URL.

OK, I see that this isnt what you meant. You could probably use a regular expression, but using substring is the easiest method. Why dont you want to use it?

1 Comment

(the unformatted original post was misleading - your answer was my first thought too - see how the question looks now)
0

If the string will always be similar, a quick-fix would be to use a StringTokenizer on the single quote. Then you would simply take the 2nd token.

1 Comment

The string may not similar always.

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.