如何在 Java 中搜索一串键/值对
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9677349/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to search a string of key/value pairs in Java
提问by user438293456
I have a String that's formatted like this:
我有一个格式如下的字符串:
"key1=value1;key2=value2;key3=value3"
"key1=value1;key2=value2;key3=value3"
for any number of key/value pairs.
对于任意数量的键/值对。
I need to check that a certain key exists (let's say it's called "specialkey"). If it does, I want the value associated with it. If there are multiple "specialkey"s set, I only want the first one.
我需要检查某个键是否存在(假设它被称为“特殊键”)。如果是这样,我想要与之关联的值。如果设置了多个“specialkey”,我只想要第一个。
Right now, I'm looking for the index of "specialkey". I take a substring starting at that index, then look for the index of the first =
character. Then I look for the index of the first ;
character. The substring between those two indices gives me the value associated with "specialkey".
现在,我正在寻找“specialkey”的索引。我取一个从该索引开始的子字符串,然后查找第一个=
字符的索引。然后我查找第一个;
字符的索引。这两个索引之间的子字符串为我提供了与“specialkey”相关的值。
This is not an elegant solution, and it's really bothering me. What's an elegant way of finding the value that corresponds with "specialkey"?
这不是一个优雅的解决方案,它真的困扰着我。找到与“specialkey”对应的值的优雅方法是什么?
采纳答案by icyrock.com
Use String.split:
使用String.split:
String[] kvPairs = "key1=value1;key2=value2;key3=value3".split(";");
This will give you an array kvPairs
that contains these elements:
这将为您提供一个kvPairs
包含以下元素的数组:
key1=value1
key2=value2
key3=value3
Iterate over these and split them, too:
迭代这些并拆分它们:
for(String kvPair: kvPairs) {
String[] kv = kvPair.split("=");
String key = kv[0];
String value = kv[1];
// Now do with key whatever you want with key and value...
if(key.equals("specialkey")) {
// Do something with value if the key is "specialvalue"...
}
}
回答by mathematical.coffee
If it's just the one key you're after, you could use regex \bspecialkey=([^;]+)(;|$)
and extract capturing group 1:
如果它只是您想要的一个键,则可以使用正则表达式\bspecialkey=([^;]+)(;|$)
并提取捕获组 1:
Pattern p = Pattern.compile("\bspecialkey=([^;]+)(;|$)");
Matcher m = p.matcher("key1=value1;key2=value2;key3=value3");
if (m.find()) {
System.out.println(m.group(1));
}
If you're doing something with the other keys, then split on ;
and then =
within a loop - no need for regex.
如果您正在使用其他键执行某些操作,;
则=
在循环中拆分然后拆分- 不需要正则表达式。
回答by Scott Weaver
Regex is well suited to matching and parsing at the same time:
正则表达式非常适合同时匹配和解析:
//c#
string input = @"key1=value1;specialkey=scott;key3=value3";
var specialkey = Regex.Match(input, "specialkey=(.*?);").Groups[1].Value;
Console.WriteLine(specialkey);
回答by Mike Sickler
I would parse the String into a map and then just check for the key:
我会将字符串解析为映射,然后只检查键:
String rawValues = "key1=value1;key2=value2;key3=value3";
Map<String,String> map = new HashMap<String,String>();
String[] entries = rawValues.split(";");
for (String entry : entries) {
String[] keyValue = entry.split("=");
map.put(keyValue[0],keyValue[1]);
}
if (map.containsKey("myKey") {
return map.get("myKey");
}
回答by asgs
Just in case anyone is interested in a pure Regex-based approach, the following snippet works.
以防万一有人对基于正则表达式的纯方法感兴趣,以下代码段有效。
Pattern pattern = Pattern.compile("([\w]+)?=([\w]+)?;?");
Matcher matcher = pattern.matcher("key1=value1;key2=value2;key3=value3");
while (matcher.find()) {
System.out.println("Key - " + matcher.group(1) + " Value - " + matcher.group(2);
}
Output will be
输出将是
Key - key1 Value - value1
Key - key2 Value - value2
Key - key3 Value - value3
However, as others explained before, String.split()
is recommended any day for this sort of task. You shouldn't complicate your life trying to use Regex when there's an alternative to use.
但是,正如其他人之前解释的那样,String.split()
建议在任何一天执行此类任务。当有替代使用时,您不应该尝试使用 Regex 使您的生活复杂化。