题解 | #字符串匹配#
字符串匹配
https://www.nowcoder.com/practice/fbdc522ef958455687654b38a4ca01e0
import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while (scanner.hasNext()) { int n = scanner.nextInt(); String[] strs = new String[n]; for (int i = 0; i < n; i++) { strs[i] = scanner.next(); } String regex = scanner.next(); Pattern pattern = Pattern.compile(regex.toLowerCase()); for (int i = 0; i < n; i++) { Matcher matcher = pattern.matcher(strs[i].toLowerCase()); if (matcher.find()) System.out.println(i + 1 + " " + strs[i]); } } } }
直接把模式串视为正则表达式