I have the following entry in a properties file:
some.key = \n
[1:Some value] \n
[14:Some other value] \n
[834:Yet another value] \n
I am trying to parse it using a regular expression, but I can't seem to get the grouping correct. I am trying to print out a key/value for each entry. Example: Key="834", Value="Yet another value"
private static final String REGEX_PATTERN = "[(\\d+)\\:(\\w+(\\s)*)]+";
private void foo(String propValue){
final Pattern p = Pattern.compile(REGEX_PATTERN);
final Matcher m = p.matcher(propValue);
while (m.find()) {
final String key = m.group(0).trim();
final String value = m.group(1).trim();
System.out.println(String.format("Key[%s] Value[%s]", key, value));
}
}
The error I get is:
Exception: java.lang.IndexOutOfBoundsException: No group 1
I thought I was grouping correctly in the regex but I guess not. Any help would be appreciated!
Thanks
UPDATE: Escaping the brackets worked. Changed the pattern to the followingThanks for the feedback!
private static final String REGEX_PATTERN = "\\[(\\d+)\\:(\\w+(\\w|\\s)*)\\]+";