41

How to extract string from "(" and ")" using pattern matching or anything. For example if the text is

"Hello (Java)"

Then how to get only "Java"?

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
Rajeev Sahu
  • 1,732
  • 6
  • 26
  • 39
  • 1
    Propably starting with `indexOf`: http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#indexOf%28int%29 – PeterMmm Jun 17 '14 at 05:44
  • 6
    What have you tried so far? What do you want to happen with nested brackets, e.g. "Hello (Foo and (Bar))"? What about situations with multiple brackets values, such as "Hello (Foo) and (Bar)"? – Jon Skeet Jun 17 '14 at 05:44

4 Answers4

91

Try this:

String x = "Hello (Java)";
Matcher m = Pattern.compile("\\((.*?)\\)").matcher(x);
while (m.find()) {
    System.out.println(m.group(1));
}

or

String str = "Hello (Java)";
String answer = str.substring(str.indexOf("(")+1, str.indexOf(")"));
Ted Klein Bergman
  • 9,146
  • 4
  • 29
  • 50
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
  • What if my input String has the same schema, but I want to extract both parts of it. ```str1="Hello "``` and ```str2="Java"```. How can I achieve it? – belostoky Apr 27 '18 at 12:49
43
List<String> matchList = new ArrayList<String>();
Pattern regex = Pattern.compile("\\((.*?)\\)");
Matcher regexMatcher = regex.matcher("Hello This is (Java) Not (.NET)");

while (regexMatcher.find()) {//Finds Matching Pattern in String
   matchList.add(regexMatcher.group(1));//Fetching Group from String
}

for(String str:matchList) {
   System.out.println(str);
}

OUTPUT

Java
.NET

What does \\((.+?)\\) mean?

This regular Expression pattern will start from \\( which will match ( as it is reserved in regExp so we need escape this character,same thing for \\) and (.*?) will match any character zero or more time anything moreover in () considered as Group which we are finding.

akash
  • 22,664
  • 11
  • 59
  • 87
  • 2
    how to get strings in the Expression ["1","5","7"]. The result should be "1", "5" and "7". Should we use like: `Pattern regex = Pattern.compile("\\[(.*?)\\]");` – Rajeev Sahu Jun 18 '14 at 08:23
  • @user1182217 You can use `Pattern regex = Pattern.compile("(\".*?\")");` and use matcher to fetch grop 1. – akash Jun 18 '14 at 08:52
  • Why is the index 1 ? – Divesh Dec 05 '22 at 16:45
  • 1
    @Divesh Good question. Actually, index 0 represents the whole pattern and to capture group index starts from 1. You can refer to official documentation for more details. Hope this answers your question. – akash Dec 11 '22 at 03:09
19

there is something even simpler than using regex:

String result = StringUtils.substringBetween(str, "(", ")");

In your example, result would be returned as "Java". I would recommend the StringUtils library for various kinds of (relatively simple) string manipulation; it handles things like null inputs automatically, which can be convenient.

Documentation for substringBetween(): https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/StringUtils.html#substringBetween-java.lang.String-java.lang.String-java.lang.String-

There are two other versions of this function, depending on whether the opening and closing delimiters are the same, and whether the delimiter(s) occur(s) in the target string multiple times.

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
Yury
  • 722
  • 7
  • 14
1

You should actually be using

List<String> matchList = new ArrayList<String>();
Pattern regex = Pattern.compile("\\(([^()]*)\\)");
Matcher regexMatcher = regex.matcher("Hello This is (Java) Not (.NET)");

while (regexMatcher.find()) {
   matchList.add(regexMatcher.group(1));
}

for(String str:matchList) {
   System.out.println(str);
}

The \(([^()]*)\) regex matches:

  • \( - a ( char
  • ([^()]*) - Group 1: any zero or more chars other than ( and )
  • \) - a ) char.

The [^()] is a negated character class that makes it impossible to match another ( after initial ( is matched with \(, thus, it ensures the innermost matches between two pairs of parentheses.

See the Java demo online and the regex demo.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563