JAVA CCF-201409-3 字符串匹配

欢迎访问我的CCF认证解题目录


题目描述

在这里插入图片描述


思路过程

这题直接使用正则表达式,如果是0就设置为忽略大小写模式,直接进行匹配输出即可


代码

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
	
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		String str = in.next();
		int type = in.nextInt(), n = in.nextInt();
		Pattern p; Matcher m;
		if ( type == 0 ) {
			p = Pattern.compile(str, Pattern.CASE_INSENSITIVE);//忽略大小写
		} else p = Pattern.compile(str);
		while ( n-- != 0 ) {
			String text = in.next();
			m = p.matcher(text);
			if ( m.find() ) {//匹配成功
				System.out.println(text);
			}
		}
	}
	
}

不会正则?戳这里:字符串处理

发布了60 篇原创文章 · 获赞 0 · 访问量 2136

猜你喜欢

转载自blog.csdn.net/weixin_43732798/article/details/103468849