How to search a string of key/value pairs in Java

2019-04-29 04:56发布

I have a String that's formatted like this:

"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.

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".

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"?

5条回答
迷人小祖宗
2楼-- · 2019-04-29 05:35

Use String.split:

String[] kvPairs = "key1=value1;key2=value2;key3=value3".split(";");

This will give you an array kvPairs that contains these elements:

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"...
   }
}
查看更多
唯我独甜
3楼-- · 2019-04-29 05:38

If it's just the one key you're after, you could use regex \bspecialkey=([^;]+)(;|$) and extract capturing group 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.

查看更多
戒情不戒烟
4楼-- · 2019-04-29 05:41

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.

查看更多
放荡不羁爱自由
5楼-- · 2019-04-29 05:47

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");
}
查看更多
来,给爷笑一个
6楼-- · 2019-04-29 05:49

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);
查看更多
登录 后发表回答