从list中模糊查询字符串

从list中模糊查询字符串

public class Test008 {

    public static void main(String[] args) {

        String name = "b";
        List<String> list = new ArrayList<>();
        list.add("aa");
        list.add("ab");
        list.add("bb");
        list.add("bc");
        List<String> search = search(name, list);
        System.out.println(search);
    }

    /**
     * 从list中模糊查询字符串
     */
    public static List<String> search(String name, List<String> list) {
        List<String> results = new ArrayList();
        Pattern pattern = Pattern.compile(name);
        for (String s : list) {
            Matcher matcher = pattern.matcher(s);
            if (matcher.find()) {
                results.add(s);
            }
        }
        return results;
    }
}

输出结果
在这里插入图片描述

posted @ 2019-12-25 16:45  smileLS66  阅读(98)  评论(0编辑  收藏  举报