题解 | #字符串通配符#

字符串通配符

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

题解

将输入的正则字符串替换为java的正则字符串

代码

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));
        }
    }
}
全部评论
点赞
送花
回复
分享
发布于 2022-07-12 10:16

相关推荐

1 1 评论
分享
牛客网
牛客企业服务