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

/**
 * @author bab
 */
public class Hello {

    public static void main(String[] args) {
        String str1 = "https://xxx:8080/xxx/check_info";
        String str2 = "https://xxx:8080/xxx/check_info";
        String regex = "com:\\d.*?/";
        System.out.println(contains(regex, str1));
    }

    /**
     * 匹配是否包含
     * @param regex 正则表达式
     * @param source 需要查找的字符串
     * @return 是否包含
     */
    public static boolean contains(String regex, String source) {
        Pattern pattern = Pattern.compile(regex);
        return pattern.matcher(source).find();
    }

    /**
     * 全匹配
     * @param regex 正则表达式
     * @param source 需要查找的字符串
     * @return 是否全匹配
     */
    public static boolean containsAll(String regex, String source) {
        return Pattern.matches(regex, source);
    }
    
    /**
     * 查找满足正则匹配条件的第一个字符串
     * @param regex 正则表达式
     * @param source 需要查找的字符串
     * @return 查找结果
     */
    public static String findStr(String regex, String source) {
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(source);
        if (matcher.find()) {
            return matcher.group();
        }
        return null;
	}

    /**
     * 查找满足正则匹配条件的所有字符串
     *
     * @param regex  正则表达式
     * @param source 需要查找的字符串
     * @return 查找结果集
     */
    public static List<String> findStrList(String regex, String source) {
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(source);
        List<String> matchStr = new ArrayList<>();
        while (matcher.find()) {
            matchStr.add(matcher.group());
        }
        return matchStr;
    }
}

更多推荐

Java使用正则表达式判断是否包含指定内容