If you mean the input will be like:

This is 2 and this is 2564
This is 42 and this is 12

then you can use the Pattern and Matcher classes.