How do I get a string which is between paranthesis using a regular expression in JAVA? Eg: if I have a string abc(de)gh Then I want the "de" Substing.
Asked
Active
Viewed 635 times
0
-
What have you tried so far? And what is the connection to PHP (your tags)? – Hubert Grzeskowiak Jan 14 '18 at 12:59
-
I have tried a regex like /\(([^()]*)\)/i – Tirth Trivedi Jan 14 '18 at 13:07
2 Answers
1
Regex: \((?<TEXT>[^()]+)\)
or (?<=\()[^()]+(?=\))
Details:
\(
matches the character (
(?<TEXT>)
Named Capture Group TEXT
[^()]+
Match a single characteras not present in the list
+
Matches between one and unlimited times
\)
matches the character )

Srdjan M.
- 3,310
- 3
- 13
- 34
0
Try this
public void test1() {
String str = "abc(de)gh(il)jk";
String regex = "\\((.*?)\\)";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(str);
while (matcher.find()) {
System.out.println(matcher.group(1));
}
}

fanfanta
- 181
- 14