How can I get inside parentheses value in a string?
android, java
Solution
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
Problem
How can I get inside parentheses value in a string? ``` String str= "United Arab Emirates Dirham (AED)"; ``` I need only AED text.