首页

基于spring-core的工具类PatternMatchUtils实现字符及数组串格式按照设定patterns类型简单查找匹配源码

标签:字符串匹配,springframework,PatternMatchUtils,工具类     发布时间:2017-12-12   

一、前言

基于spring-core(4.1.4)的org.springframework.util.PatternMatchUtils工具类,可以根据指定字符串匹配类型格式pattern对字符串及字符串数组内容进行简单的查找匹配处理simpleMatch,从而判断是否存在。具体代码参见下面所示。

二、源码说明

package org.springframework.util;@b@@b@public abstract class PatternMatchUtils@b@{@b@  public static boolean simpleMatch(String pattern, String str)@b@  {@b@    if ((pattern == null) || (str == null))@b@      return false;@b@@b@    int firstIndex = pattern.indexOf(42);@b@    if (firstIndex == -1)@b@      return pattern.equals(str);@b@@b@    if (firstIndex == 0) {@b@      if (pattern.length() == 1)@b@        return true;@b@@b@      int nextIndex = pattern.indexOf(42, firstIndex + 1);@b@      if (nextIndex == -1)@b@        return str.endsWith(pattern.substring(1));@b@@b@      String part = pattern.substring(1, nextIndex);@b@      int partIndex = str.indexOf(part);@b@      while (partIndex != -1) {@b@        if (simpleMatch(pattern.substring(nextIndex), str.substring(partIndex + part.length())))@b@          return true;@b@@b@        partIndex = str.indexOf(part, partIndex + 1);@b@      }@b@      return false;@b@    }@b@@b@    return ((str.length() >= firstIndex) && @b@      (pattern@b@      .substring(0, firstIndex)@b@      .equals(str.substring(0, firstIndex))) && @b@      (simpleMatch(pattern@b@      .substring(firstIndex), @b@      str.substring(firstIndex))));@b@  }@b@@b@  public static boolean simpleMatch(String[] patterns, String str)@b@  {@b@    int i;@b@    if (patterns != null)@b@      for (i = 0; i < patterns.length; ++i)@b@        if (simpleMatch(patterns[i], str))@b@          return true;@b@@b@@b@@b@    return false;@b@  }@b@}