Java Regular Expression to Extract Content Within Square Brackets

Java regular expression to extract content within square or round brackets

You can write a regex that doesn't need to have alternation and can have only one group which you can uniquely access to get the value and even better if you use positive lookarounds to just capture your intended value using this regex,

(?<=[([])[^()[\]]*(?=[)\]])

Explanation:

  • (?<=[([]) - Positive look behind ensuring the preceding character is either ( or [
  • [^()[\]]* - Matches any character except opening or closing parenthesis
  • (?=[)\]]) - Positive look ahead to ensure it matches either ) or ]

Demo

Sample Java codes,

String s = "Example_(xxxxx)_AND_(yyyyy)_2019-01-28";
Pattern p = Pattern.compile("(?<=[(\\[])[^()\\[\\]]*(?=[)\\]])");
Matcher m = p.matcher(s);
while (m.find()) {
System.out.println(m.group());
}

Prints,

xxxxx
yyyyy

Alternatively, like I mentioned above, you can use this non-look around regex and capture just the group1 to get your content as this regex doesn't have any alternation hence only one group.

[([]([^()[\]]*)[)\]]

Demo without lookaround regex

Sample Java code with non-look around regex where you need to capture using group(1)

String s = "Example_(xxxxx)_AND_(yyyyy)_2019-01-28";
Pattern p = Pattern.compile("[(\\[]([^()\\[\\]]*)[)\\]]");
Matcher m = p.matcher(s);
while (m.find()) {
System.out.println(m.group(1));
}

Prints,

xxxxx
yyyyy

Regular expression to extract text between square brackets

You can use the following regex globally:

\[(.*?)\]

Explanation:

  • \[ : [ is a meta char and needs to be escaped if you want to match it literally.
  • (.*?) : match everything in a non-greedy way and capture it.
  • \] : ] is a meta char and needs to be escaped if you want to match it literally.

java regular expression to extract content within square brackets

A bit more concise:

String in = "Item(s): [item1.test],[item2.qa],[item3.production]";

Pattern p = Pattern.compile("\\[(.*?)\\]");
Matcher m = p.matcher(in);

while(m.find()) {
System.out.println(m.group(1));
}

Regex to extract the string both inside and outside a square bracket

You can use this regex, to match the data as per your sample in post,

\[(?:[^\]]*)\]|([a-zA-Z]+(?:\s+[a-zA-Z]+)*)

There are two sub-regex in it as alternation, where \[(?:[^\]]*)\] will capture any text that will be of form [somedata] and ([a-zA-Z]+(?:\s+[a-zA-Z]+)*) regex will capture data of form somedata or somedata somemoredata somemoredatafurther

Demo

Sample Java codes,

String s = "[Sometext]MoreText[SomeOtherText] I am hoping to get [SomeText], MoreText, [SomeOtherText]";
Pattern p = Pattern.compile("\\[(?:[^\\]]*)\\]|([a-zA-Z]+(?:\\s+[a-zA-Z]+)*)");
Matcher m = p.matcher(s);
while (m.find()) {
System.out.println(m.group());
}

Prints,

[Sometext]
MoreText
[SomeOtherText]
I am hoping to get
[SomeText]
MoreText
[SomeOtherText]

Pattern to extract text between parenthesis

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(")"));


Related Topics



Leave a reply



Submit