将 key=value 的字符串解析为 Map

Parse a String of key=value to a Map(将 key=value 的字符串解析为 Map)
本文介绍了将 key=value 的字符串解析为 Map的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在使用一个提供 XML 的 API,我需要从一个实际上是字符串的标签中获取地图.示例:

I'm using an API that gives me a XML and I need to get a map from one tag which is actually a string. Example:

拥有

Billable=7200,Overtime=false,TransportCosts=20$

我需要

["Billable"="7200","Overtime=false","TransportCosts"="20$"]

问题是字符串是完全动态的,所以,它可以像

The problem is that the string is totally dynamic, so, it can be like

Overtime=true,TransportCosts=one, two, three
Overtime=true,TransportCosts=1= 1,two, three,Billable=7200

所以我不能只用逗号分隔,然后用等号分隔.是否可以使用正则表达式将类似的字符串转换为地图?

So I can not just split by comma and then by equal sign. Is it possible to convert a string like those to a map using a regex?

到目前为止我的代码是:

My code so far is:

private Map<String, String> getAttributes(String attributes) {
    final Map<String, String> attr = new HashMap<>();
    if (attributes.contains(",")) {
        final String[] pairs = attributes.split(",");
        for (String s : pairs) {
            if (s.contains("=")) {
                final String pair = s;
                final String[] keyValue = pair.split("=");
                attr.put(keyValue[0], keyValue[1]);
            }
        }
        return attr;
    }
    return attr;
}

提前致谢

推荐答案

你可以用

(w+)=(.*?)(?=,w+=|$)

请参阅 正则表达式演示.

详情

  • (w+) - 第 1 组:一个或多个单词字符
  • = - 一个等号
  • (.*?) - 第 2 组:除换行符以外的任何零个或多个字符,尽可能少
  • (?=,w+=|$) - 需要 ,,然后是 1+ 个单词字符,然后是 =,或字符串的结尾紧挨当前位置的右侧.
  • (w+) - Group 1: one or more word chars
  • = - an equal sign
  • (.*?) - Group 2: any zero or more chars other than line break chars, as few as possible
  • (?=,w+=|$) - a positive lookahead that requires a ,, then 1+ word chars, and then =, or end of string immediately to the right of the current location.

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

Java 测试:

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

这篇关于将 key=value 的字符串解析为 Map的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)
How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)
Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)
Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)
How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)
How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)