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

cn.handyplus.lib.core.PatternUtil Maven / Gradle / Ivy

The newest version!
package cn.handyplus.lib.core;

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

/**
 * 正则工具类
 *
 * @author handy
 * @since 3.9.6
 */
public class PatternUtil {

    private PatternUtil() {
    }

    /**
     * 正则判断是否包含字符
     *
     * @param input 输入字符串
     * @param str   被包含的字符
     * @return true 包含
     */
    public static boolean contains(String input, String str) {
        Pattern pattern = Pattern.compile("(?i).*" + Pattern.quote(str) + ".*");
        Matcher matcher = pattern.matcher(input);
        return matcher.matches();
    }

    /**
     * 正则判断替换第一个包含的字符
     *
     * @param input 输入字符串
     * @param str   被包含的字符
     * @return 返回被替换的字符
     */
    public static String replaceFirst(String input, String str) {
        return input.replaceFirst("(?i)" + Pattern.quote(str), "").trim();
    }

    /**
     * 正则判断替换所有包含的字符
     *
     * @param input 输入字符串
     * @param str   被包含的字符
     * @return 返回被替换掉对的字符
     */
    public static String replaceAll(String input, String str) {
        return input.replaceAll("(?i)" + Pattern.quote(str), "").trim();
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy