模糊匹配字符的正则表达式+例子

本文介绍了一个实用的正则表达式工具类,包括IP地址验证、字符串模糊匹配及排除特定字符串的功能。提供了具体实现代码及示例,帮助读者理解和应用正则表达式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package com.cn.util;


import java.util.regex.Pattern;


/**
 * 正则表达式 工具类
 * 
 * @author lifangyu
 */
public class RegexUtil {


    /*
     * IP地址的匹配标达式 ( // \\d{1,3}) // :\d // 0~9数字,{1,3} // 至少一位,最多三位)
     */
    private static String regex_IP = "^(121.15.215.(\\d{1,3}))$";
    /*
     * 字符串 模糊匹配 :^(.*张三.*name.*)$ ; 等值匹配 ^(张三)$
     */
    private static String regex_containStr = "^(.*张三.*name.*)$";
    /*
     * 字符不包含特定字符串的表达式
     */
    private static String regex_notcontainStr = "^(?!.*(转发)).*$";// 不包含特定字符串的表达式


    public static void main(String[] args) {
        System.out.println(StringMatchRule("这个邮件 是转发的!", regex_notcontainStr));
    }


    public static boolean StringMatchRule(String souce, String regex) {
        boolean result = false;
        if (regex != null && souce != null) {
            result = Pattern.matches(regex, souce);
        }


        return result;
    }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值