HashMap搜索和匹配正则表达式

时间:2011-12-09 19:08:37

标签: java regex search hashmap match

我正在尝试搜索HashMap并将特定字符串与表示字符串的节点匹配。

为了匹配完全具体的值,它将按照以下方式进行:

       for (Entry<String, Component> entry : hashMap.entrySet()) {
            if(entry.getValue().toString().matches("test1")){
                System.out.println(entry.getValue().toString());
            }
            else
            {
                System.out.println("Nothing found");
            }
        }

但我有不同的情况。该节点包含长字符串"xxx .. test 1 .."

那么,如何将“test 1”与这些节点字符串匹配?

2 个答案:

答案 0 :(得分:2)

您可以使用indexOf:

if ( entry.getValue().toString().indexOf("test 1") != -1 ) {

答案 1 :(得分:0)

如果你需要做的就是查看字符串中是否有test 1,你可以使用String.contains()方法。

if(entry.getValue().toString().contains("test1")){
   System.out.println(entry.getValue().toString());
}
else
{
   System.out.println("Nothing found");
}
相关问题