题解 | #字符串通配符#
Java使用正则表达式的一个demo。
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNext()) { String regex = sc.next().toLowerCase(); String str = sc.next().toLowerCase(); // * 匹配0个或以上的数字英文字符 (注意:需要考虑连续出现多个*的情况) regex = regex.replaceAll("\\*{1,}", "[0-9A-Za-z]*"); // ? 匹配1个数字英文字符 regex = regex.replaceAll("\\?", "[0-0A-Za-z]{1}"); System.out.println(str.matches(regex)); } } }