Home  >  Article  >  Web Front-end  >  How to implement the fuzzy matching function of regular expressions

How to implement the fuzzy matching function of regular expressions

php中世界最好的语言
php中世界最好的语言Original
2018-03-29 15:38:363233browse

这次给大家带来正则表达式的模糊匹配功能如何实现,实现正则表达式模糊匹配功能的注意事项有哪些,下面就是实战案例,一起来看一下。

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

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;
  }
}

相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!

推荐阅读:

js中的正则表达式如何使用(附代码)

验证身份证号与和邮箱以及判断checked的选中的正则是什么样的

The above is the detailed content of How to implement the fuzzy matching function of regular expressions. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn