Extracting string from within round brackets in Java with regex

后端 未结 4 1190
迷失自我
迷失自我 2020-12-07 04:46

I\'m trying to extract a string from round brackets.
Let\'s say, I have John Doe (123456789) and I want to output the string 123456789 only.

4条回答
  •  感情败类
    2020-12-07 05:18

    this works for me :

    @Test
    public void myTest() {
        String test = "test (mytest)";
        Pattern p = Pattern.compile("\\((.*?)\\)");
        Matcher m = p.matcher(test);
    
        while(m.find()) {
            assertEquals("mytest", m.group(1));
        }
    }
    

提交回复
热议问题