How can I get inside parentheses value in a string?

Compiles and prints “AED”. Even works for multiple parenthesis:

import java.util.regex.*;

public class Main
{
  public static void main (String[] args)
  {
     String example = "United Arab Emirates Dirham (AED)";
     Matcher m = Pattern.compile("\\(([^)]+)\\)").matcher(example);
     while(m.find()) {
       System.out.println(m.group(1));    
     }
  }
}

The regex means:

  • \\(: character (
  • (: start match group
  • [: one of these characters
  • ^: not the following character
  • ): with the previous ^, this means “every character except )
  • +: one of more of the stuff from the [] set
  • ): stop match group
  • \\): literal closing paranthesis

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)