有什么方法可以在 Java 中匹配 Iterable<String> 中的模式吗?

Are there any way to match a pattern in Iterable<String> in Java?

我正在尝试匹配 Iterable<String> text 的模式,要求是找出不以 abc 开头的文本,我尝试使用正则表达式 text.matches(^(?!abc).+),但似乎这些是可迭代的 matches 方法。 `

您需要迭代您拥有的 Iterable。例如:

for (String t : text) {
    if (t.matches("^(?!abc).+") {
        // do something with it
    }
}

您也可以这样做并捕获匹配的字符串。

List<String> strs =
        List.of("abcapple", "abcfoo", "abcbar", "abwordc");

strs = strs.stream().filter(str -> str.matches("^(?!abc).+"))
        .collect(Collectors.toList());

System.out.println(strs);

版画

[abwordc]