Problem with your regex is that by default * quantifier is greedy, which means it will try to match as much as he can. Since you used it with .* it means that it will try to match max of any characters since that is what . represents (except line separators).
So your regex .*\((\d+)\).* will match
ABC 123-(456)-(789) .* -^^^^^^^^^^^^^^ ((\d+)\) ---------------^^^^^ .* -empty
To change behavior of * and make it reluctant add ? like .*?.
But in your case it looks like you should remove .* from your regex since you probably don't actually want to match part which they describe. So try with
String regex = "\\((\\d+)\\)";
and for string like "ABC 123-(456)-(789)" you should get result 456 - since it is first result matching this regex. To move to text result 789 you need to use find method again.
So your code can look like:
private static final Pattern p = Pattern.compile("\\((\\d+)\\)"); //we make Pattern static field since to avoid recompiling //same pattern each time we call our method static String myFind(String text){ Matcher m = p.matcher(text); if (m.find()){ return m.group(1); }else{ return null;//or empty string, or maybe throw exception } }
...may contain some important information. Post minimal but complete code which will let us reproduce your problem.[0-9].