在字符串集合中使用通配符进行java搜索
我有一个HashMap<Integer,String>
。我尝试了以下代码来查询地图并返回所有可能的值
public Collection<String> query(String queryStr) {
List<String> list = new ArrayList<String>();
for (Map.Entry<String, Integer> entry : myMap.entrySet()) {
if (queryStr.matches(entry.getKey()))
list.add(entry.getKey());
}
if (list.isEmpty())
return null;
else
return list;
}
如果映射有"test","best","crest","zest","testy","tether","temper","teat","tempest"
。对te*t
的查询应返回"teat","tempest","test"
。对于“test*”,它应该返回“test”、“testy”。如何实施?字符串是否有通配符搜索?我不能使用任何外部库
# 1 楼答案
匹配器
\w*
只搜索以下字符:[a-zA-Z_0-9]
如果要使用
*
匹配器搜索所有字符,则应尝试以下操作: