Java regular expression: if the closing bracket is the last character in the string, match any number of digits in the parentheses

I need some help to save my day (or my night) I want to match:

>Any number of numbers > enclosed in parentheses "()" [there is nothing else in parentheses except numbers] > if the closing parenthesis ")" is the last character in the string

This is the code I proposed:

// 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 solve. I can only set magic expressions that match any number, but if they are enclosed in parentheses and at the end of the line, please don't help me

thank you!

Solution

You can try this regular expression:

String regex = "\\(\\d+\\)$";
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>