简体   繁体   English

Java:d​​o-while(正则表达式),hashtable.containsKey()可能吗?

[英]Java: do-while(regex), hashtable.containsKey() possible?

Can I use a Regular Expression with Hashtable.containsKey(Object value); 我可以使用正则表达式和Hashtable.containsKey(对象值);

Is there any way to do the following pseudo-code? 有什么办法可以做下面的伪代码?

while ( myHashtable.containsKey(regex) ) {
    //TODO
};

Where my regex would be "ERROR[0-9][0-9]?" 我的正则表达式是“ERROR [0-9] [0-9]?”

edit: I'm setting any number of error messages from a backend Java program which gets sent to the JSP as a hashtable. 编辑:我从后端Java程序设置任意数量的错误消息,后者将作为哈希表发送到JSP。 The JSP needs to display these error messages from the hashtable. JSP需要从哈希表中显示这些错误消息。 Using the hashtable in the JSP is non-negotiable. 在JSP中使用哈希表是不可协商的。 I just need to know how to make sure we read all of the error messages. 我只需要知道如何确保我们读取所有错误消息。

Not in the way you're suggesting. 并非您所建议的那样。

Hash tables use an Object 's hashCode and equals methods to quickly find the object you are looking for. 散列表使用ObjecthashCodeequals方法来快速找到您要查找的对象。

Instead you can iterate through the hash table's elements and look for matches like this: 相反,您可以遍历哈希表的元素并查找这样的匹配:

for (Map.Entry<String,Object> entry : myHashtable.entrySet){
    if (regex.matcher(entry.getKey()).matches()) {
        //TODO
    }
}

I haven't tested any of this but since the function you want to use expects an Object and you want to use a regular expression, you need an object that is a regular expression. 我没有测试任何这个,但由于你想要使用的函数需要一个Object而你想使用正则表达式,你需要一个正则表达式的对象。

You could give the object Pattern a shot: http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html 你可以给对象Pattern一个镜头: http//docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html

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

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