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
How to Calculate a Time Span in Java and Format the Output
Is There a Difference Between Single and Double Quotes in Java
Java.Lang.Nullpointerexception Is Thrown Using a Method-Reference But Not a Lambda Expression
Jdk9: an Illegal Reflective Access Operation Has Occurred. Org.Python.Core.Pysystemstate
List All Files from a Directory Recursively with Java
Read Whole Text Files from a Compression in Spark
Why Shouldn't I Call Setvisible(True) Before Adding Components
Converting a Java Keystore into Pem Format
@Aspectj Pointcut for All Methods of a Class with Specific Annotation
Hadoop No Filesystem for Scheme: File
File Upload with Java (With Progress Bar)
What Does It Mean to Program to an Interface
Windows Shortcut (.Lnk) Parser in Java
How to Configure Jackson in Wildfly
What Is Difference Between Crudrepository and JPArepository Interfaces in Spring Data JPA
Get Only Part of an Array in Java
Converting Integers to Roman Numerals - Java
Retrieving the Inherited Attribute Names/Values Using Java Reflection