题解 | 字符串通配符

字符串通配符

https://www.nowcoder.com/practice/43072d50a6eb44d2a6c816a283b02036

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNext()) { // 注意 while 处理多个 case

            String pattern = in.nextLine();
            String target = in.nextLine();
            String p_new = pattern.replace("*", "[a-zA-Z0-9]*").replace("?", "[a-zA-Z0-9]?").replace("[a-zA-Z0-9]*[a-zA-Z0-9]*[a-zA-Z0-9]*", "[a-zA-Z0-9]{0,3}").replace("[a-zA-Z0-9]*[a-zA-Z0-9]*", "[a-zA-Z0-9]{0,2}").toUpperCase();
            System.out.println(target.toUpperCase().matches(p_new));
        }
    }
}

全部评论

相关推荐

自由水:笑死了,敢这么面试不敢让别人说
点赞 评论 收藏
分享
评论
2
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务