题解 | #字符串通配符#
字符串通配符
https://www.nowcoder.com/practice/43072d50a6eb44d2a6c816a283b02036
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
//https://blog.csdn.net/u012230798/article/details/86487304
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
while (in.hasNextLine()) {
String s1 = in.nextLine();
String s2 = in.nextLine();
s1 = s1.toLowerCase();
s2 = s2.toLowerCase();
String regx = s1.replaceAll("\\*{2,}",
"\\*"); //将出现次数2次及以上的*替换成一个*
regx = regx.replaceAll("\\?", "[0-9a-z]{1}"); //将?替换为0-9或者a-z
regx = regx.replaceAll("\\*", "[0-9a-z]{0,}");//将*替换为0-9或a-z多次
//System.out.println(regx); regx变为了正则表达式
System.out.println(s2.matches(regx)); //判断s2是否匹配正则表达式regx
}
}
}
科大讯飞公司氛围 431人发布