正則表達式工具類

一、代碼

package com.citydo.xclouddesk.utils;


import com.google.gson.JsonParseException;
import com.google.gson.JsonParser;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.oro.text.regex.MalformedPatternException;
import org.apache.oro.text.regex.Pattern;
import org.apache.oro.text.regex.PatternCompiler;
import org.apache.oro.text.regex.PatternMatcher;
import org.apache.oro.text.regex.Perl5Compiler;
import org.apache.oro.text.regex.Perl5Matcher;
import java.util.ArrayList;
import java.util.List;

/**
 * @author nick
 */
@Slf4j
public class RegularUtils {


    /**
     * 判斷一個字符串是否符合正則格式 舉例: regExp=你好(中國|世界) text=你好世界或者你好中國 返回 true
     * @param text 文本
     * @param regExp 正則表達式
     * @return
     */
    public static boolean isRegular(String text,String regExp){
        if (StringUtils.isBlank(text) || StringUtils.isBlank(regExp)) {
            return false;
        }
        return java.util.regex.Pattern.matches(regExp, text);
    }


    /**
     * 校驗字符串是否是 json 格式
     * @param json 文本
     * @return
     */
    public static boolean isJson(String json) {
        if (StringUtils.isBlank(json)) {
            return false;
        }
        try {
            new JsonParser().parse(json);
            return true;
        } catch (JsonParseException e) {
            return false;
        }
    }


    /**
     * 自定義正則
     * @param text   文本
     * @param regExp 正則表達式
     * @param regularSet 自定義正則表達式
     * @return
     */
    public static boolean isExcluded(String text, String regExp, List<Pattern> regularSet){
        if (StringUtils.isBlank(text) || StringUtils.isBlank(regExp)) {
            return false;
        }
        return false;
    }

    /**
     * LGF正則  舉例 (今天|明天|後天)北京[的]天氣[預報]    true 今天北京的天氣預報  false 前天北京的天氣預報
     * @param text 匹配的源字符串
     * @param regExp 匹配的正規表達式
     * @return 如果源字符串符合要求返回真,否則返回假
     */
    public static boolean isHardRegexpValidate(String text, String regExp) {
        if (StringUtils.isBlank(text) || StringUtils.isBlank(regExp)) {
            return false;
        }
        try {
            // 用於定義正規表達式對象模板類型
            PatternCompiler compiler = new Perl5Compiler();
            // 正規表達式比較批配對象
            PatternMatcher matcher = new Perl5Matcher();
            // 實例大小大小寫敏感的正規表達式模板
            Pattern hardPattern = compiler.compile(regExp);
            // 返回批配結果
            return matcher.contains(text, hardPattern);
        } catch (MalformedPatternException e) {
            log.info("解析異常:{}",e.getMessage());
        }
        return false;
    }

    /**
     * 判斷是否匹配正則
     *
     * @param regex 正則表達式
     * @param input 要匹配的字符串
     * @return {@code true}: 匹配
     *         {@code false}: 不匹配
     */
    public static boolean isMatch(String regex, CharSequence input) {
        return input != null && input.length() > 0 && java.util.regex.Pattern.matches(regex, input);
    }

    /**
     * 獲取正則匹配的部分
     *
     * @param regex 正則表達式
     * @param input 要匹配的字符串
     * @return 正則匹配的部分
     */
    public static List<String> getMatches(String regex, CharSequence input) {
        if (input == null) {
            return null;
        }
        List<String> matches = new ArrayList<>();
        java.util.regex.Pattern pattern = java.util.regex.Pattern.compile(regex);
        java.util.regex.Matcher matcher = pattern.matcher(input);
        while (matcher.find()) {
            matches.add(matcher.group());
        }
        return matches;
    }

    /**
     * 獲取正則匹配分組
     *
     * @param input 要分組的字符串
     * @param regex 正則表達式
     * @return 正則匹配分組
     */
    public static String[] getSplits(String input, String regex) {
        if (input == null) {
            return null;
        }
        return input.split(regex);
    }

    /**
     * 替換正則匹配的第一部分
     *
     * @param input       要替換的字符串
     * @param regex       正則表達式
     * @param replacement 代替者
     * @return 替換正則匹配的第一部分
     */
    public static String getReplaceFirst(String input, String regex, String replacement) {
        if (input == null) {
            return null;
        }
        return java.util.regex.Pattern.compile(regex).matcher(input).replaceFirst(replacement);
    }

    /**
     * 替換所有正則匹配的部分
     *
     * @param input       要替換的字符串
     * @param regex       正則表達式
     * @param replacement 代替者
     * @return 替換所有正則匹配的部分
     */
    public static String getReplaceAll(String input, String regex, String replacement) {
        if (input == null) {
            return null;
        }
        return java.util.regex.Pattern.compile(regex).matcher(input).replaceAll(replacement);
    }



//    public static void main(String[] args) {
//        System.out.println(isHardRegexpValidate("今天北京的天氣預報","(今天|明天|後天)北京[的]天氣[預報]"));
//        System.out.println(isRegular("你好中國","你好(中國|世界)"));
//    }
}


二、依賴

       <dependency>
            <groupId>oro</groupId>
            <artifactId>oro</artifactId>
            <version>2.0.7</version>
        </dependency>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章