Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Regular Expression: match any number of digits in round brackets if the closing bracket is the last char in the String

I need some help to save my day (or my night). I would like to match:

  1. Any number of digits
  2. Enclosed by round brackets "()" [The brackets contain nothing else than digits]
  3. If the closing bracket ")" is the last character in the String.

Here's the code I have come up with:

// this how the text looks, the part I want to match are the digits in the brackets at the end of it
    String text = "Some text 45 Some text, text and text (1234)";  
    String regex = "[no idea how to express this.....]"; // this is where the regex should be
            Pattern regPat = Pattern.compile(regex);
            Matcher matcher = regPat.matcher(text);

            String matchedText = "";

            if (matcher.find()) {
                matchedText = matcher.group();
            }

Please help me out with the magic expression I have only managed to match any number of digits, but not if they are enclosed in brackets and are at the end of the line...

Thanks!

like image 891
Kovács Imre Avatar asked Dec 26 '22 15:12

Kovács Imre


1 Answers

You can try this regex:

String regex = "\\(\\d+\\)$";
like image 133
anubhava Avatar answered Dec 28 '22 07:12

anubhava