Java RegEx – Extract string between brackets example shows how to extract string data between brackets (square bracket or round bracket – parentheses) using Java regular expression pattern.
How to extract string data between brackets using regex?
Suppose you want to extract the number “105” from the text “Total files found: [105] out of 200”. That number is between two square brackets.
Since the square brackets are used to define a character class in the regular expressions, we need to escape brackets using double backslash characters to match it literally. Plus, since we want to extract a number between the opening and closing bracket, we are going to use the “\\d” in the pattern as given below.
1 |
\\[\\d+\\] |
Here the “\\d+” means one or more digits. Let’s try this pattern in the code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
package com.javacodeexamples.regex; import java.util.regex.Matcher; import java.util.regex.Pattern; public class RegExExtractBetweenBrackets { public static void main(String[] args) { String str = "Total files found: [105] out of 200"; Pattern pattern = Pattern.compile("\\[\\d+\\]"); Matcher matcher = pattern.matcher(str); if( matcher.find() ) { System.out.println( matcher.group() ); } } } |
Output
1 |
[105] |
As you can see from the output, our code extracted the number between square brackets, but it included the brackets as well. In order to get just the number, we need to define a regex group, so that we can extract just that group in our code as given below.
1 2 3 4 5 6 7 8 |
String str = "Total files found: [105] out of 200"; Pattern pattern = Pattern.compile("\\[(\\d+)\\]"); Matcher matcher = pattern.matcher(str); if( matcher.find() ) { System.out.println( matcher.group(1) ); } |
Output
1 |
105 |
In the above example, we wanted to extract a number so we used the pattern “\\d”. But instead of a number, if you want to extract other text data, you can extract any text between the brackets using the “\\[(.*)\\]” pattern including numbers. See below given example.
1 2 3 4 5 6 7 8 |
String str = "Total files found: [105] out of 200"; Pattern pattern = Pattern.compile("\\[(.*)\\]"); Matcher matcher = pattern.matcher(str); if( matcher.find() ) { System.out.println( matcher.group(1) ); } |
Output
1 |
105 |
That worked as well. But wait, what about the string “Total files found: [105] out of [200]”? Now the string content has two square brackets. Let’s try that with the same regex pattern.
1 2 3 4 5 6 7 8 |
String str = "Total files found: [105] out of [200]"; Pattern pattern = Pattern.compile("\\[(.*)\\]"); Matcher matcher = pattern.matcher(str); if( matcher.find() ) { System.out.println( matcher.group(1) ); } |
Output
1 |
105] out of [200 |
Strange, right? Actually no, the “.*” is greedy in nature. It means it consumes all the text till the last instance of the ending square bracket “]”. So instead of a greedy expression i.e. “.*”, we need to use a reluctant expression i.e. “.*?” as given below.
1 2 3 4 5 6 7 8 |
String str = "Total files found: [105] out of [200]"; Pattern pattern = Pattern.compile("\\[(.*?)\\]"); Matcher matcher = pattern.matcher(str); while( matcher.find() ) { System.out.println( matcher.group(1) ); } |
Output
1 2 |
105 200 |
How to extract string between Round bracket or parentesis?
It can be done in the same way. Just like the square bracket, we also need to escape the round brackets. Everything else remains same as given below.
1 2 3 4 5 6 7 8 |
String str = "Total files found: (105) out of (200), (processed)"; Pattern pattern = Pattern.compile("\\((.*?)\\)"); Matcher matcher = pattern.matcher(str); while( matcher.find() ) { System.out.println( matcher.group(1) ); } |
Output
1 2 3 |
105 200 processed |
If you want to learn more about the regex pattern, please visit the Java RegEx Tutorial.
Please let me know your views in the comments section below.