我正在使用正则表达式来尝试匹配 INI 文件中的节块。我正在使用《Regular Expressions Cookbook 》一书中给出的食谱,但它似乎对我不起作用。
这是我正在使用的代码:
final BufferedReader in = new BufferedReader(
new FileReader(file));
String s;
String s2 = "";
while((s = in.readLine())!= null)
s2 += s + System.getProperty("line.separator");
in.close();
final String regex = "^\\[[^\\]\r\n]+](?:\r?\n(?:[^\r\n].*)?)*";
final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
String sectionBlock = null;
final Matcher regexMatcher = pattern.matcher(s2);
if (regexMatcher.find()) {
sectionBlock = regexMatcher.group();
}
这是我的输入文件的内容:
[Section 2]
Key 2.0=Value 2.0
Key 2.2=Value 2.2
Key 2.1=Value 2.1
[Section 1]
Key 1.1=Value 1.1
Key 1.0=Value 1.0
Key 1.2=Value 1.2
[Section 0]
Key 0.1=Value 0.1
Key 0.2=Value 0.2
Key 0.0=Value 0.0
问题是sectionBlock
最终等于文件的全部内容,而不仅仅是第一部分。
(我不知道这是否重要,但我在 Windows 上执行此操作,并且其中的行分隔符s2
等于“\r\n”(至少,IDEA 调试器将它们显示为)。)
我在这里做错了什么?