繁体   English   中英

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

[英]String filter ignoring regular expressions

我想过滤字符串列表但忽略正则表达式。 例如:寻找“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
测试.xy

下面给出一个例子:

import java.util.Arrays;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        List<String> list = Arrays.asList("test.xy", "abctest.xy", "testaxy", "testy");
        for (String s : list) {
            if (s.contains("test.xy"))
                System.out.println(s);
        }

        // Display using Stream
        System.out.println("\nFilter and display using Stream:");
        list.stream().filter(s -> s.contains("test.xy")).forEach(System.out::println);
    }
}

输出:

test.xy
abctest.xy

Filter and display using Stream:
test.xy
abctest.xy

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM