我必須解析具有鍵值對的屬性文件,並且可能會對某些行進行註釋(!或#,兩者均有效)。正則表達式讀取prop文件條目不能正確捕獲組
了Exa:
key1 val1
Key2 val2
#key3 val3
# key4 val4
# It does not have = symbol
# Spaces can be any where.
...
如果行沒有被註釋掉然後讀取鍵和值匹配的組值。我用下面的正則表達式和代碼片段,但它不是捕獲鍵和值如預期:
String inputs[] = {
"key1 val1",
"Key2 val2",
"#key3 val3",
" # key4 val4"
};
Pattern PATTERN = Pattern.compile("^(\\s*[^#!]\\s*)(\\w*)\\s+(\\w*).*$");
for (int i = 0; i < inputs.length; i++) {
System.out.println("Input: " + inputs[i]);
Matcher matcher = PATTERN.matcher(inputs[i]);
if(matcher.matches()) {
int groupCount = matcher.groupCount();
if(groupCount > 0) {
for (int j = 1; j <= groupCount; j++) {
System.out.println(j + " " + matcher.group(j));
}
} else {
System.out.println(matcher.group());
}
} else {
System.out.println("No match found.");
}
System.out.println("");
}
這裏是輸出:
Input: key1 val1
1 k
2 ey1
3 val1
Input: Key2 val2
1 K
2 ey2
3 val2
Input: #key3 val3
No match found.
Input: # key4 val4
No match found.
我的想法是:
^ - Start of line
(\\s*[^#!]\\s*) - space(s) followed by NO # or ! followed by space(s)
(\\w*) - Key
\\s+ - spaces(s)
(\\w*) - Value
.* - Anything
$ - End of line
請幫我瞭解這裏出了什麼問題。 爲什麼它捕獲鍵的第一個字符作爲一個組?
爲什麼不使用Java屬性文件? –
+1 to @JavierDiaz:Java屬性文件具有您描述的語法,您不需要解析它,只需將其作爲屬性文件讀取即可。 – fge