您可以使用
(\w+)=(.*?)(?=,\w+=|$)
请参阅regex演示。
Java代码:
public static Map<String, String> getAttributes(String attributes) {
Map<String, String> attr = new HashMap<>();
Matcher m = Pattern.compile("(\\w+)=(.*?)(?=,\\w+=|$)").matcher(attributes);
while (m.find()) {
attr.put(m.group(1), m.group(2));
}
return attr;
}
String s = "Overtime=true,TransportCosts=1= 1,two, three,Billable=7200";
Map<String,String> map = getAttributes(s);
for (Map.Entry entry : map.entrySet()) {
System.out.println(entry.getKey() + "=" + entry.getValue());
}
结果:
Overtime=true
Billable=7200
TransportCosts=1= 1,two, three