引入java的俩个类:Pattern.class、Matcher.class
String likeValue = "yx.*";/* likeValue - 自定义通配符格式*/
Pattern p = Pattern.compile(likeValue );
Matcher m = null;
m = p.matcher(isLikeValue );/* isLikeValue - 需要匹配的参数*/
if (m.matches()){
logger.info(" matcher --> " + isLikeValue );
}
含Map、List匹配:
public static List<String> isLikeValueByMap(Map<String, String>map,String likeValue){
List<String> list=new Vector<>();
for (Map.Entry<String, String> entity : map.entrySet()) {
if(entity.getKey().indexOf(likeValue)>-1){
list.add((String) entity.getValue());
}
}
return list;
}
调用此封装的方法like:List<?> list=isLikeValueByMap(map,"likeValue");(其中? - 代表任一类型、likeValue - 自定义匹配字符串格式。)
通配符匹配>>>
public static boolean wildcardMatch(String pattern, String str, String wildcard) { if (StringUtils.isEmpty(pattern) || StringUtils.isEmpty(str)) { return false; } final boolean startWith = pattern.startsWith(wildcard); final boolean endWith = pattern.endsWith(wildcard); String[] array = StringUtils.split(pattern, wildcard); int currentIndex = -1; int lastIndex = -1 ; switch (array.length) { case 0: return true ; case 1: currentIndex = str.indexOf(array[0]); if (startWith && endWith) { return currentIndex >= 0 ; } if (startWith) { return currentIndex + array[0].length() == str.length(); } if (endWith) { return currentIndex == 0 ; } return str.equals(pattern) ; default: for (String part : array) { currentIndex = str.indexOf(part); if (currentIndex > lastIndex) { lastIndex = currentIndex; continue; } return false; } return true; } }
调用此封装的方法>>>
public static boolean simpleWildcardMatch(String pattern, String str) { return wildcardMatch(pattern, str, "*"); }