3

I'm trying to get first match for number in brackets and expect to get 123, but:

String str = "ABC 123-(456)-(789)"; String regex = ".*\\((\\d+)\\).*"; Pattern p = Pattern.compile(regex); Matcher m = p.matcher(str); m.find(); System.out.println(m.group(1)); // "789" 

Please help.

I found workaround but without matcher:

String[] arr = str.split("[\\(\\)]"); for (String s : arr) { if (s.matches("[-+]?\\d+")) { return s; } } 
8
  • 2
    Something tells me that ... may contain some important information. Post minimal but complete code which will let us reproduce your problem. Commented Dec 23, 2015 at 16:22
  • why dont you just split the string with space then return the index of 1 with regex of [0-9]. Commented Dec 23, 2015 at 16:23
  • Sure. It's my firs post so i did some mistakes. Commented Dec 23, 2015 at 16:24
  • Do update your question use edit option. Commented Dec 23, 2015 at 16:25
  • Your code doesn't compile so it can't produce any results. Take your time and post proper SSCCE. Commented Dec 23, 2015 at 16:38

2 Answers 2

6

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 } } 
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for detailed explanation. It's is useful.
2

Try this regex \\b((?<number>\\d+)\\b) like this:

String str = "ABC (123) (456) (678)" ; Pattern p = Pattern.compile("\\b((?<number>\\d+)\\b)"); Matcher m = p.matcher(str); m.find(); System.out.println(m.group()); 

Output:

123 

3 Comments

you might want to upvote it and accept it if it solved your problem. This is our way of saying "Thanks. It helped" ;) check the basic guidelines there: stackoverflow.com/tour
@jopasserat OP can't up-vote anything yet (it requires 15 reputation: stackoverflow.com/help/privileges/vote-up). But you are right about accepting answer.
Ok. I'm new here. And i vote when i have enough reputation.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.