从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;
}
}
输出结果