is string a Media Url by regex - Java java.util.regex

Java examples for java.util.regex:Match URL

Description

is string a Media Url by regex

Demo Code

import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.regex.Pattern;

public class Main{
    public static void main(String[] argv){
        String input = "java2s.com";
        System.out.println(isMediaUrl(input));
    }/*  www  .j a v a2s . c  o  m*/
    
    public static boolean isMediaUrl(String input) {
        return isRtmpUrl(input) || isHttpUrl(input);
    }
    
    public static boolean isRtmpUrl(String input) {
        if (isEmpty(input)) {
            return false;
        }
        input = removeLastSlashChar(input);
        Pattern pattern = Pattern
                .compile("rtmp://(\\w+(-\\w+)*)(\\.(\\w+(-\\w+)*))*(:\\d{2,4})?(\\/[^#$]+)*");
        return pattern.matcher(input).matches();
    }
    
    public static boolean isHttpUrl(String input) {
        if (isEmpty(input))
            return false;
        Pattern pattern = Pattern
                .compile("http://(\\w+(-\\w+)*)(\\.(\\w+(-\\w+)*))*(:\\d{2,4})?(\\/[^#$]+)*");
        return pattern.matcher(input).matches();
    }
    
    public static boolean isEmpty(String str) {
        return (str == null || str.trim().length() == 0);
    }
    
    public static String removeLastSlashChar(String str) {
        if (str == null) {
            return null;
        }
        if (str.endsWith("/")) {
            return str.substring(0, str.length() - 1);
        }
        return str;
    }
    
    public static String trim(String str) {
        return (str == null) ? null : str.trim();
    }
    
    public static String substring(String origin, String begin, String end) {
        if (origin == null) {
            return origin;
        }
        int beginIndex = (begin == null) ? 0 : origin.indexOf(begin)
                + begin.length();
        int endIndex = (end == null) ? origin.length() : origin.indexOf(
                end, beginIndex);
        if (endIndex == -1) {
            return origin.substring(beginIndex);
        }
        return origin.substring(beginIndex, endIndex);
    }
    
    public static String substring(String origin, String begin) {
        return substring(origin, begin, null);
    }
}

Related Tutorials