本文實例講述了正則表達式實現(xiàn)字符的模糊匹配功能。分享給大家供大家參考,具體如下:
package com.cn.util;import java.util.regex.Pattern;/** * 正則表達式 工具類 * * @author lifangyu */public class RegexUtil { /* * IP地址的匹配標達式 ( // //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.VeVB.COm/regex/javascript
正則表達式在線生成工具:
http://tools.VeVB.COm/regex/create_reg
希望本文所述對大家正則表達式學習有所幫助。
新聞熱點
疑難解答
圖片精選