All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.emily.infrastructure.common.RegexPathMatcher Maven / Gradle / Ivy

There is a newer version: 5.0.0
Show newest version
package com.emily.infrastructure.common;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 正则表达式匹配
 *
 * @author Emily
 * @since Created in 2023/7/2 1:13 PM
 */
public class RegexPathMatcher {
    private static final Map CACHE = new HashMap<>();

    /**
     * 判断给定的URL请求路径是否与指定的正则表达式匹配
     *
     * @param pattern 正则表达式
     * @param path    请求URL
     * @return true-请求路径符合正则表达式,false-请求路径不符合正则表达式
     */
    public static Matcher matcher(String pattern, String path) {
        if (!CACHE.containsKey(pattern)) {
            // 编译正则表达式
            CACHE.put(pattern, Pattern.compile(pattern));
        }
        // 匹配操作
        return CACHE.get(pattern).matcher(path);
    }

    /**
     * 判定给定的URL请求路径是否与指定的正则表达式配置
     *
     * @param patterns 正则表达式集合
     * @param path     请求URL
     * @return 如果有一个正则表达式匹配,则返回true;否则返回false
     */
    public static boolean matcherAny(List patterns, String path) {
        for (int i = 0; i < patterns.size(); i++) {
            if (matcher(patterns.get(i), path).matches()) {
                return true;
            }
        }
        return false;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy