I have to get index value from the following string:
Home.number[12].parent.
I want to get back value of 12.
Here is what I tried:
//escape . / [ ]
private static final String pattern = "Home\\.number\\[([0-9]*)\\]*";
private static final Pattern addressPattern = Pattern.compile(pattern);
private static int getIndex(String input, Pattern pattern){
Matcher m = pattern.matcher(input);
if (m.matches()){
return Integer.valueOf(m.group(2));
}
return -1;
}
public static void main(String[] args){
System.out.println(getIndex("Home.number[123].parent", addressPattern);
System.out.println(getIndex("Home.number[456].child", addressPattern);
}
I get back -1 for both, meaning no match is found.
Using the debugger, I found that m.matches() is returning false.
I am unable to figure out why.
P.S: I also tried using Pattern.quote("Home.number[([0-9]*])*") and StringUtils.EscapeJava("Home.number[([0-9]*)]*"). Both are not returning any matching results.