字符串通配符
字符串通配符
http://www.nowcoder.com/questionTerminal/43072d50a6eb44d2a6c816a283b02036
我承认我有投机取巧的成分,但是用正则表达式来做的话会很简单。
暂且就认为测试用例和题目上的差不多,正则表达式中只含有"? * ."这三种,将其替换为对应的就可以了。
import java.util.Scanner;
/**
* 实现通配符
* ? 匹配一个字符 [0-9A-Za-z]{1}
* * 匹配0个或以上的字符 [0-9A-Za-z]{0,}
* (字符由英文字母和数字0-9组成,不区分大小写。下同)
*/
public class Test71 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNextLine()) {
String regx = scanner.nextLine();
String string = scanner.nextLine();
//做相应的替换
regx = regx.replaceAll("\\?", "[0-9A-Za-z]{1}");
regx = regx.replaceAll("\\*", "[0-9A-Za-z]{0,}");
regx = regx.replaceAll("\\.", "\\\\.");
boolean result = string.matches(regx);
System.out.println(result);
}
}
}拿来测试,用例全部通过。
查看15道真题和解析