问题描述:?匹配任何(仅)一个字符,*匹配0或多个字符
样例输入:te?t*.* txt12.xls
样例输出:false
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext()) {
String reg = scanner.nextLine();
String str = scanner.nextLine();
System.out.println(match(reg, str));
}
scanner.close();
}
private static boolean match(String reg, String str) {
return match(reg, 0, str, 0);
}
private static boolean match(String reg, int i, String str, int j) {
// 正则式已经到达末尾了
if (i >= reg.length()) {
return j >= str.length();
}
// 匹配串已经到达末尾了
if (j >= str.length()) {
return i >= str.length();
}
// 两个都没有到末尾
boolean result = false;
switch (reg.charAt(i)) {
case '*':
// 匹配一(多)个字符
result = match(reg, i, str, j + 1);
if (result) {
return true;
}
// 不匹配字符
result = match(reg, i + 1, str, j);
if (result) {
return true;
}
// 只匹配一个字符
result = match(reg, i + 1, str, j + 1);
break;
case '?':
result = match(reg, i + 1, str, j + 1);
break;
default:
if (reg.charAt(i) == str.charAt(j)) {
result = match(reg, i + 1, str, j + 1);
}
}
return result;
}
}