How to search string of key / value pairs in Java

I have a string that is formatted as follows:

"key1=value1;key2=value2;key3=value3"

for any number of key / value pairs.

I need to check that a certain key exists (let it be called "specialkey"). If so, I want a value associated with it. If there are many "special keys", I want only the first.

Now I am looking for the index "specialkey". I take the substring starting at this index, then look at the index of the first character = . Then I look for the index of the first character ; . The substring between the two indices gives me the value associated with the "special key".

This is not an elegant solution, and it really bothers me. What an elegant way to find a value that matches "specialkey"?

+4
source share
5 answers

Use String.split :

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

This will give you a kvPairs array containing these elements:

 key1=value1 key2=value2 key3=value3 

Iterations over them and their division 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"... } } 
+6
source

I would parse String on the map, and then just check 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"); } 
+7
source

If this is only one key that you use, you can use regex \bspecialkey=([^;]+)(;|$) and extract capture 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 are doing something with other keys, divide by ; and then = inside the loop - no regular expression needed.

+4
source

Just in case, if anyone is interested in a pure Regex-based approach, the following expression works. However, as others explained earlier, String.split() recommended any day for this kind of task. You should not complicate your life with Regex when there is an alternative to use.

 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); } 

The output will be

 Key - key1 Value - value1 Key - key2 Value - value2 Key - key3 Value - value3 
+1
source

Regex is well suited for 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); 
0
source

Source: https://habr.com/ru/post/1401089/


All Articles