题解 | #字符串通配符#

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));
        }
    }
}


全部评论

相关推荐

头像
04-02 20:00
点赞 评论 收藏
转发
点赞 收藏 评论
分享
牛客网
牛客企业服务