There are a lot of answers to this question in stackoverflow. Next time search before you ask!
Here is a simple code which gives what you want:
String str= "animal 1 animal";
Pattern p = Pattern.compile("-?\\d+");
Matcher match = p.matcher(str);
while (match.find()) {
System.out.println(match.group());
}
It is the same with your other Strings.. just change the value of “str” or create another variables.
solved Get number from the string (RegExp) [closed]