Java – use regexp to extract values between parentheses

Before management, I try to extract the value between parentheses (and) to check the existence of the value Please help me extract it

Pattern pattern;
         pattern = Pattern.compile("\\b(.*\\b)");
             Matcher matcher = pattern.matcher(node.toString());
             if (matcher.find()){
                System.out.println();// here I need to print value that I find between brackets
             }

Solution

Escape parentheses in regular expressions:

Pattern pattern = Pattern.compile("\\((.*?)\\)");

Then you can do this:

Matcher matcher = pattern.matcher(node.toString());
if (matcher.find()){
    System.out.println( matcher.group(1) );
}
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
分享
二维码
< <上一篇
下一篇>>