I am trying to parse windows ini file using java on windows. Suppose the contents are:
[section1] key1=value1 key2=value2 [section2] key1=value1 key2=value2 [section3] key1=value1 key2=value2
I am using the following code:
Pattern pattSections = Pattern.compile("^\\[([a-zA-Z_0-9\\s]+)\\]$([^\\[]*)", Pattern.DOTALL + Pattern.MULTILINE); Pattern pattPairs = Pattern.compile("^([a-zA-Z_0-9]+)\\s*=\\s*([^$]*)$", Pattern.DOTALL + Pattern.MULTILINE); // parse sections Matcher matchSections = pattSections.matcher(content); while (matchSections.find()) { String keySection = matchSections.group(1); String valSection = matchSections.group(2); // parse section content Matcher matchPairs = pattPairs.matcher(valSection); while (matchPairs.find()) { String keyPair = matchPairs.group(1); String valPair = matchPairs.group(2); } }
But it does not work correctly:
Section1 does not match. Probably because it does not start with "after EOL". When I put an empty line before [section1]
, it matches.
valSection
returns '\ r \ nke1 = value1 \ r \ nkey2 = value2 \ r \ n'. keyPair
returns 'key1'. Sounds good. But valPair
returns the value 'value1 \ r \ nkey2 = value2 \ r \ n', but not the value 'value1'.
What is wrong here?
source share