【问题标题】:How to to create a key/value map from a string in java (more complicated) [closed]如何从java中的字符串创建键/值映射(更复杂)[关闭]
【发布时间】:2012-07-13 21:44:11
【问题描述】:

我有这样的文字

"SimpleKey1: word1. SimpleKey2Word1 SimpleKey2Word2: word word word, word word. word word. CompoundKey3 / CompoundKey3: word word word, word. Key3: word. CompoundKey4Word1 CompoundKey4Word2 / CompoundKey4Word3 CompoundKey4Word4: word words, words word-word (word 18 word 100 ). CompoundKey5 / CompoundKey5: word word."

我需要解析该字符串以获得键/值映射,例如:

SimpleKey1: word1. 
SimpleKey2Word1 SimpleKey2Word2: word word word, word word. word word. 
CompoundKey3 / CompoundKey3: word word word, word. 
CompoundKey4Word1 CompoundKey4Word2 / CompoundKey4Word3 CompoundKey4Word4: word words,    words word-word (word 18 word 100 ).  
CompoundKey5 / CompoundKey5: word word.

请注意,键可以包含斜杠字符 (/),值可以包含特殊字符。

我不知道应该使用什么正则表达式。

谢谢。

【问题讨论】:

  • 给定键的值列表的结尾是什么?
  • @m0skit0 - 似乎每个值都以有效键(字母、数字、空格和斜杠)之前的最后一个句点和空格结尾。
  • 我们应该从您的示例中猜测规范,还是您可以提供规范?

标签: java regex string-matching


【解决方案1】:
String data = "SimpleKey1: word1. SimpleKey2Word1 SimpleKey2Word2: word "
            + "word word, word word. word word. CompoundKey3 / CompoundKey3: "
            + "word word word, word. Key3: word. CompoundKey4Word1 "
            + "CompoundKey4Word2 / CompoundKey4Word3 CompoundKey4Word4: word "
            + "words, words word-word (word 18 word 100 ). CompoundKey5 / "
            + "CompoundKey5: word word.";
Pattern p=Pattern.compile("([\\w\\s/]+):(.*?)(?=$|([\\w\\s/]+):)");
Matcher m=p.matcher(data);
while(m.find())
    System.out.println(m.group().trim());

输出:

SimpleKey1: word1.
SimpleKey2Word1 SimpleKey2Word2: word word word, word word. word word.
CompoundKey3 / CompoundKey3: word word word, word.
Key3: word.
CompoundKey4Word1 CompoundKey4Word2 / CompoundKey4Word3 CompoundKey4Word4: word words, words word-word (word 18 word 100 ).
CompoundKey5 / CompoundKey5: word word.

如果您想获得密钥,请使用m.group(1)。对于价值,您可以使用m.group(2) 喜欢

while(m.find()){
    System.out.println("key=>"+m.group(1));
    System.out.println("value=>"+m.group(2));
}

【讨论】:

    【解决方案2】:

    试试这个正则表达式(.+?)(?=\.\s*(([A-Z])|($)))

    【讨论】:

      猜你喜欢
      • 2019-11-14
      • 2019-05-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-05
      • 2021-08-30
      • 2012-02-11
      相关资源
      最近更新 更多