正則表達式實現(xiàn)字符的模糊匹配功能示例
更新時間:2017年05月12日 10:59:57 作者:新方之玉
這篇文章主要介紹了正則表達式實現(xiàn)字符的模糊匹配功能,結(jié)合具體java實例形式分析了針對字符串的模糊匹配查詢相關(guān)實現(xiàn)技巧,需要的朋友可以參考下
本文實例講述了正則表達式實現(xiàn)字符的模糊匹配功能。分享給大家供大家參考,具體如下:
package com.cn.util; import java.util.regex.Pattern; /** * 正則表達式 工具類 * * @author lifangyu */ public class RegexUtil { /* * IP地址的匹配標(biāo)達式 ( // \\d{1,3}) // :\d // 0~9數(shù)字,{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 = "^(?!.*(轉(zhuǎn)發(fā))).*$";// 不包含特定字符串的表達式 public static void main(String[] args) { System.out.println(StringMatchRule("這個郵件 是轉(zhuǎn)發(fā)的!", 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; } }
PS:這里再為大家提供2款非常方便的正則表達式工具供大家參考使用:
JavaScript正則表達式在線測試工具:
http://tools.jb51.net/regex/javascript
正則表達式在線生成工具:
http://tools.jb51.net/regex/create_reg
希望本文所述對大家正則表達式學(xué)習(xí)有所幫助。
相關(guān)文章
JScript中正則表達函數(shù)的說明與應(yīng)用
JScript中正則表達函數(shù)的說明與應(yīng)用...2007-04-04