Android String to URL Convert extractUrls(String text)

Here you can find the source of extractUrls(String text)

Description

extract Urls

Declaration

public static List<String> extractUrls(String text) 

Method Source Code

//package com.java2s;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    private static Pattern urlPattern = Pattern
            .compile("((?:(http|https|Http|Https):\\/\\/"
                    + "(?:(?:[a-zA-Z0-9\\$\\-\\_\\.\\+\\!\\*\\'\\(\\)\\,\\;\\?\\&\\=]|"
                    + "(?:\\%[a-fA-F0-9]{2})){1,64}(?:\\:(?:[a-zA-Z0-9\\$\\-\\_\\.\\"
                    + "+\\!\\*\\'\\(\\)\\,\\;\\?\\&\\=]|(?:\\%[a-fA-F0-9]{2})){1,25})"
                    + "?\\@)?)?((?:(?:[a-zA-Z0-9][a-zA-Z0-9\\-\\_]{0,64}\\.)"
                    + "+(?:(?:aero|arpa|asia|a[cdefgilmnoqrstuwxz])"
                    + "|(?:biz|b[abdefghijmnorstvwyz])|(?:cat|com|"
                    + "coop|c[acdfghiklmnoruvxyz])|d[ejkmoz]|(?:edu|e[cegrstu])|f[ijkmor]"
                    + "|(?:gov|g[abdefghilmnpqrstuwy])|h[kmnrtu]|(?:info|int|i[delmnoqrst])"
                    + "|(?:jobs|j[emop])|k[eghimnrwyz]|l[abcikrstuvy]|"
                    + "(?:mil|mobi|museum|m[acdeghklmnopqrstuvwxyz])|"
                    + "(?:name|net|n[acefgilopruz])|(?:org|om)|(?:pro|p[aefghklmnrstwy])|"
                    + "qa|r[eouw]|s[abcdeghijklmnortuvyz]|(?:tel|travel|t[cdfghjklmnoprtvwz])"
                    + "|u[agkmsyz]|v[aceginu]|w[fs]|y[etu]|z[amw]))|(?:(?:25[0-5]|2[0-4][0-9]"
                    + "|[0-1][0-9]{2}|[1-9][0-9]|[1-9])\\.(?:25[0-5]|2[0-4][0-9]|[0-1][0-9]{2}"
                    + "|[1-9][0-9]|[1-9]|0)\\.(?:25[0-5]|2[0-4][0-9]|[0-1][0-9]{2}|[1-9][0-9]"
                    + "|[1-9]|0)\\.(?:25[0-5]|2[0-4][0-9]|[0-1][0-9]{2}|[1-9][0-9]|[0-9])))"
                    + "(?:\\:\\d{1,5})?)(\\/(?:(?:[a-zA-Z0-9\\;\\/\\?\\:\\@\\&\\=\\#\\~%\\-\\."
                    + "\\+\\!\\*\\'\\(\\)\\,\\_])|(?:\\%[a-fA-F0-9]{2}))*)?");

    public static List<String> extractUrls(String text) {
        List<String> urls = new ArrayList<String>();

        Matcher matcher = urlPattern.matcher(text);
        while (matcher.find()) {
            urls.add(matcher.group());/*from ww w.j a  va  2s . c  o m*/
        }

        return urls;
    }
}

Related

  1. getYoutubeId(String url)
  2. preUrl(String url)
  3. TruncateUrlPage(String strURL)
  4. isValidUrl(String url)
  5. imageUrl_2_ImageName(String imageUrl)
  6. isUrlValid(String str, boolean isCaseSensitve)