正则表达式实现字符的模糊匹配功能示例

时间:2021-05-02

本文实例讲述了正则表达式实现字符的模糊匹配功能。分享给大家供大家参考,具体如下:

? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 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; } }

希望本文所述对大家正则表达式学习有所帮助。

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章