类似win下搜索文件的匹配方式
?:代表0个或1个任意字符
*:代表0个或多个任意字符
例子:
abcdefg
?ab* ->true
ab* ->true
ab*c* ->true
ab*d??????*****g->true
abc ->false
------解决方案--------------------
- -这必然是正则
------解决方案--------------------
------解决方案--------------------
- Java code
public class StringCompare { static int cmp; static void strcmp(String str1, int sub1, String str2, int sub2){ if(sub1 >= str1.length() || sub2 >= str2.length()){ if(sub1 == str1.length() && sub2 == str2.length()){ ++cmp; return; } else if(sub1 == str1.length()){ int i; for(i = sub2; i < str2.length(); ++i){ if(str2.charAt(i) != '?' && str2.charAt(i) != '*'){ return; } } if(i == str2.length()) ++cmp; } return; } if(str2.charAt(sub2) == '?'){ strcmp(str1, sub1, str2, ++sub2); strcmp(str1, ++sub1, str2, sub2); } if(sub2 < str2.length() && str2.charAt(sub2) == '*'){ for(int i = 0; i < str1.length(); ++i){ strcmp(str1, sub1+i, str2, sub2+1); } } if(sub1 < str1.length() && sub2 < str2.length()){ if(str1.charAt(sub1) != str2.charAt(sub2)){ return; } else strcmp(str1, sub1+1, str2, sub2+1); } } public static void main(String[] args){ String str1 = new String("abcdefg"); String str2 = new String("ab*d??????*****g"); strcmp(str1, 0, str2, 0); System.out.println(cmp >= 1); }}
------解决方案--------------------
有必要把正则的逻辑再实现一次吗?可以找下jdk中java.util.regx.*的源码看下