RegexpUtils

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author ParanoidCAT
 */
public class RegexpUtils {
    /**
     * 模板缓存
     */
    private static final Map<String, Pattern> PATTERN_CACHE = new ConcurrentHashMap<>(32);

    private RegexpUtils() throws Exception {
        throw new Exception("no RegexpUtils instance should be created");
    }

    /**
     * 从缓存中获取正则表达式模板
     *
     * @param expression 表达式
     * @return 表达式模板
     */
    private static Pattern getPattern(String expression) {
        if (PATTERN_CACHE.containsKey(expression)) {
            return PATTERN_CACHE.get(expression);
        } else {
            Pattern current = Pattern.compile(expression, Pattern.CASE_INSENSITIVE);
            Pattern previous = PATTERN_CACHE.putIfAbsent(expression, current);
            if (previous == null) {
                return current;
            } else {
                current = null;
                return previous;
            }
        }
    }

    /**
     * 判断表达式是否出现在原文中
     *
     * @param expression 表达式
     * @param prototype  原文
     * @return true/false
     */
    public static boolean expressionFound(String expression, String prototype) {
        try {
            return getPattern(expression).matcher(prototype).find();
        } catch (Exception e) {
            return false;
        }
    }

    /**
     * 判断原文是否完全符合表达式
     *
     * @param expression 表达式
     * @param prototype  原文
     * @return true/false
     */
    public static boolean expressionMatched(String expression, String prototype) {
        try {
            return getPattern(expression).matcher(prototype).matches();
        } catch (Exception e) {
            return false;
        }
    }

    /**
     * 获取原文中符合表达式的内容
     *
     * @param expression 表达式
     * @param prototype  原文
     * @return 原文中符合表达式的内容
     */
    public static String getExpressionFound(String expression, String prototype) {
        return getExpressionFound(expression, prototype, 0);
    }

    /**
     * 获取原文中符合表达式的的内容的一部分
     *
     * @param expression 表达式
     * @param prototype  原文
     * @param groupId    表达式分组编号
     * @return 原文中符合表达式的的内容的一部分
     */
    public static String getExpressionFound(String expression, String prototype, int groupId) {
        try {
            Matcher matcher = getPattern(expression).matcher(prototype);
            return matcher.find() ? matcher.group(groupId) : null;
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 获取原文中所有符合表达式的内容
     *
     * @param expression 表达式
     * @param prototype  原文
     * @return 原文中符合表达式的内容集合
     */
    public static List<String> getAllExpressionFound(String expression, String prototype) {
        List<String> list = new ArrayList<>(16);
        try {
            Matcher matcher = getPattern(expression).matcher(prototype);
            while (matcher.find()) {
                list.add(matcher.group(0));
            }
            return list;
        } catch (Exception e) {
            return Collections.emptyList();
        }
    }

    /**
     * 获取原文中所有符合表达式的内容
     * 若无法获得,取缺省值
     *
     * @param expression 表达式
     * @param prototype  原文
     * @return 原文中符合表达式的内容或缺省值
     */
    public static String getExpressionFoundOrDefault(String expression, String prototype, String defaultValue) {
        return getExpressionFoundOrDefault(expression, prototype, 0, defaultValue);
    }

    /**
     * 获取原文中符合表达式的的内容的一部分
     * 若无法获得,取缺省值
     *
     * @param expression 表达式
     * @param prototype  原文
     * @param groupId    表达式分组编号
     * @return 原文中符合表达式的的内容的一部分或缺省值
     */
    public static String getExpressionFoundOrDefault(String expression, String prototype, int groupId, String defaultValue) {
        try {
            Matcher matcher = getPattern(expression).matcher(prototype);
            return matcher.find() ? matcher.group(groupId) : defaultValue;
        } catch (Exception e) {
            return defaultValue;
        }
    }
}

 

posted on 2019-05-23 10:44  纠结的猫  阅读(169)  评论(0)    收藏  举报

导航