gpt4 book ai didi

java - 字符串过滤器忽略正则表达式

转载 作者:行者123 更新时间:2023-12-01 17:42:13 26 4
gpt4 key购买 nike

我想过滤字符串列表但忽略正则表达式。例如:查找“test.xy”应仅显示“test.xy”或“abctest.xy”等条目,而不显示“testaxy”等条目。我不想要“。”作为通配符。

我怎样才能做到这一点?

最佳答案

如果您想过滤字符串并将它们收集到一个新列表中,您可以按如下方式操作;

    List<String> strings = List.of("mytest.xy",
"abctest.xy", "test.xy", "testaxy", "testy");
String target = "test.xy";
List<String> result = strings.stream()
.filter(str -> str.contains(target))
.collect(Collectors.toList());

result.forEach(System.out::println);

打印

mytest.xy
abctest.xy
test.xy

关于java - 字符串过滤器忽略正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59742258/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com