我有一个像
这样的文字 "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.
请注意,键可以包含斜杠字符(/),值可以包含特殊字符。
我无法弄清楚我应该使用的正则表达式。
感谢。
答案 0 :(得分:4)
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));
}
答案 1 :(得分:0)
试试这个正则表达式(.+?)(?=\.\s*(([A-Z])|($)))