How can I parse a strings like :
name1="val1" name2="val2" name3="val3"
I cannot use split(\s+)
as it can be name = "val 1"
.
I am doing java but any laguage is okay.
How can I parse a strings like :
name1="val1" name2="val2" name3="val3"
I cannot use split(\s+)
as it can be name = "val 1"
.
I am doing java but any laguage is okay.
In Python:
import re
astr='''name1="val 1 " name2 = "val2" name3="val3"'''
print(re.findall('\w+\s*=\s*".*?"',astr))
# ['name1="val 1 "', 'name2 = "val2"', 'name3="val3"']
Here it is in Java, with a slight variation on the regex, with capturing group to put the name/value pairs into a Map<String,String>
.
This usage of Matcher.find()
in a while
loop is typical.
import java.util.*;
import java.util.regex.*;
//...
String pattern = "(\\w+)\\s*=\\s*\"([^\"]*+)\"";
String text = "name1 = \"val 1\" name2=\"val2\" name3=\"val3\"";
System.out.println(text);
// name1 = "val 1" name2="val2" name3="val3"
Matcher m = Pattern.compile(pattern).matcher(text);
Map<String,String> map = new HashMap<String,String>();
while (m.find()) {
map.put(m.group(1), m.group(2));
}
System.out.println(map);
// {name3=val3, name1=val 1, name2=val2}