字符(.*)等模糊匹配搜索,通配符匹配

引入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, "*");  
   } 

 


   

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值