How can I get inside parentheses value in a string?
String str= "United Arab Emirates Dirham (AED)";
I need only AED text.
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.)
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.
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 paranthesisIf you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With