get Url String In String by regex - Android Network

Android examples for Network:URL

Description

get Url String In String by regex

Demo Code


//package com.java2s;

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

public class Main {
    public static String getUrlStrInString(String origin) {
        if (origin == null) {
            throw new NullPointerException("the param origin is null");
        }//from ww w. java  2  s .  c  om
        Pattern pattern = Pattern
                .compile("(http|ftp|https):\\/\\/[\\w\\-_]+(\\.[\\w\\-_]+)+([\\w\\-\\.,@?^=%&:/~\\+#]*[\\w\\-\\@?^=%&/~\\+#])?");
        Matcher matcher = pattern.matcher(origin);
        StringBuffer buffer = new StringBuffer();
        //while
        if (matcher.find()) {
            buffer.append(matcher.group());
        }
        return buffer.toString();
    }
}

Related Tutorials