I have a string of format "[232]......." I want to extract the 232 out of the string, I did this
public static int getNumber(String str) {
Pattern pattern = Pattern.compile("\\[([0-9]+)\\]");
Matcher matcher = pattern.matcher(str);
int number = 0;
while (matcher.find()) {
number = Integer.parseInt(matcher.group());
}
return number;
}
but it doesn't work, I got the following exception:
Exception in thread "main" java.lang.NumberFormatException: For input string: "[232]"
Anyone knows how could I solve this problem, and if there is a more efficient way for me to do this kind of pattern matching in java?