[英]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]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.