Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get inside parentheses value in a string?

Tags:

java

android

How can I get inside parentheses value in a string?

String str= "United Arab Emirates Dirham (AED)"; 

I need only AED text.

like image 922
MuraliGanesan Avatar asked Jan 29 '13 13:01

MuraliGanesan


People also ask

How do you nested parentheses?

1. Use brackets inside parentheses to create a double enclosure in the text. Avoid parentheses within parentheses, or nested parentheses. Correct: (We also administered the Beck Depression Inventory [BDI; Beck, Steer, & Garbin, 1988], but those results are not reported here.)

Can you use parentheses in regex?

By placing part of a regular expression inside round brackets or parentheses, you can group that part of the regular expression together. This allows you to apply a quantifier to the entire group or to restrict alternation to part of the regex. Only parentheses can be used for grouping.


1 Answers

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
like image 105
Janus Troelsen Avatar answered Sep 30 '22 19:09

Janus Troelsen