Assuming you want the part between single quotes, use this regular expression with a Matcher:

"'(.*?)'"

Example:

String mydata = "some string with 'the data i want' inside";
Pattern pattern = Pattern.compile("'(.*?)'");
Matcher matcher = pattern.matcher(mydata);
if (matcher.find())
{
    System.out.println(matcher.group(1));
}

Result:

the data i want
Answer from Mark Byers on Stack Overflow
🌐
Baeldung
baeldung.com › home › java › java string › get substring from string in java
Get Substring from String in Java | Baeldung
July 21, 2024 - For more details on the Java regular expressions check out this tutorial. We can use the split method from the String class to extract a substring. Say we want to extract the first sentence from the example String.
🌐
Baeldung
baeldung.com › home › java › java string › getting the text that follows after the regex match in java
Getting the Text That Follows After the Regex Match in Java | Baeldung
January 8, 2024 - Therefore, in this example, if we write a Regex pattern to match “targetValue=“, we must extract everything after the match. However, the problem could have a variant. So, let’s see another input variable: static String INPUT2 = "Some text. targetValue=Java is cool.
🌐
Medium
medium.com › stackera › java-regex-part-4-replacing-and-extracting-text-6eecdbf7f5e8
Java RegEx: Part 4— Replacing and Extracting Text | by Sera Ng. | Tech Training Space | Medium
October 18, 2020 - In this section, I’m going to show you how we can use the Scanner class in the package java.util to extract text from a string.
🌐
GeeksforGeeks
geeksforgeeks.org › java › extracting-word-string-java
Extracting each word from a String using Regex in Java - GeeksforGeeks
July 23, 2025 - // Java program to demonstrate extracting words // from string using Regex import java.util.regex.Matcher; import java.util.regex.Pattern; public class Test { public static void main(String[] args) { String s1 = "Geeks for Geeks"; String s2 = "A Computer Science Portal for Geeks"; Pattern p = Pattern.compile("[a-zA-Z]+"); Matcher m1 = p.matcher(s1); Matcher m2 = p.matcher(s2); System.out.println("Words from string \"" + s1 + "\" : "); while (m1.find()) { System.out.println(m1.group()); } System.out.println("Words from string \"" + s2 + "\" : "); while (m2.find()) { System.out.println(m2.group()); } } } Output:
🌐
DevQA
devqa.io › extract-numbers-string-java-regular-expressions
Extract Numbers From String Using Java Regular Expressions
February 11, 2020 - Suppose we have this string Sample_data = YOUR SET ADDRESS IS 6B1BC0 TEXT and we want to extract 6B1BC0 which is 6 characters long, we can use: import java.util.regex.Matcher; import java.util.regex.Pattern; public class RegexExamples { public static void main (String[] args) { Pattern p = Pattern.compile("YOUR SET ADDRESS IS\\s+([A-Z0-9]{6})"); Matcher n = p.matcher("YOUR SET ADDRESS IS 6B1BC0 TEXT"); if (n.find()) { System.out.println(n.group(1)); // Prints 123456 } } }
🌐
Coderanch
coderanch.com › t › 590292 › java › Regex-extract-substring
Regex to extract a substring (Java in General forum at Coderanch)
Or you could use the URL class and use its various getXXX methods to extract the parts you want. reply reply · Bookmark Topic Watch Topic · New Topic · Boost this thread! Similar Threads · java regex help needed · java regex group capture · Text Parsing with Regex · Regular expression to take integers out of a string ·
Find elsewhere
🌐
TutorialsPoint
tutorialspoint.com › article › java-program-to-extract-a-single-quote-enclosed-string-from-a-larger-string-using-regex
Java program to extract a single quote enclosed string from a larger string using Regex
September 16, 2024 - In the event that the pattern matches, we extract the matched string using the group() method with a parameter of 1 which is representative to the 1st capture group in the pattern. This is the drawback of this method that it does not capture all groups of single quotes enclosed substrings. import java.util.regex.Matcher; import java.util.regex.Pattern; public class StringExtractor { public static void main(String[] args) { String input = "This is a 'single quote' enclosed string"; Pattern pattern = Pattern.compile("'(.*?)'"); Matcher matcher = pattern.matcher(input); if (matcher.find()) { String extractedString = matcher.group(1); System.out.println(extractedString); } } }
🌐
Penpapernotes
penpapernotes.com › post › extract-string-using-regex-in-java
How to extract a substring after a match using regex in Java | Pen Paper Notes
March 25, 2022 - 1import java.util.regex.Matcher; 2import java.util.regex.Pattern; 3 4public class ExtractStringUsingRegex { 5 6 public static void main(String[] args) { 7 String input = "You would have received the Unique code as part of your email. Please copy and paste that code in the form link provided to you.
🌐
TutorialsPoint
tutorialspoint.com › Extracting-each-word-from-a-string-using-Regex-in-Java
How to extract each (English) word from a string using regular expression in Java?
July 21, 2020 - Therefore, to extract each word in the given input string − · Compile the above expression of the compile() method of the Pattern class. Get the Matcher object bypassing the required input string as a parameter to the matcher() method of the Pattern class. Finally, for each match get the matched characters by invoking the group() method. import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class EachWordExample { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter sample text: "); String data = s
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-program-to-extract-a-single-quote-enclosed-string-from-a-larger-string-using-regex
Java Program to Extract a Single Quote Enclosed String From a Larger String using Regex - GeeksforGeeks
July 23, 2025 - Input : "Out of this String required only is 'Geeks for Geeks' only'" Output : Geeks for Geeks Input : "The data wanted is'Java Regex'" Output : Java Regex ... // Java program to demonstrate extracting // substring enclosed in single quotes // Importing Matcher and Pattern class // from regex class of java.util package // Importing input output classes import java.io.*; import java.util.regex.Matcher; import java.util.regex.Pattern; // Class public class GFG { // Main driver method public static void main(String[] args) { // Custom input String string1 = "Out of this String I want 'Geeks for G
Top answer
1 of 1
64

The matcher.group() function expects to take a single integer argument: The capturing group index, starting from 1. The index 0 is special, which means "the entire match". A capturing group is created using a pair of parenthesis "(...)". Anything within the parenthesis is captures. Groups are numbered from left to right (again, starting from 1), by opening parenthesis (which means that groups can overlap). Since there are no parenthesis in your regular expression, there can be no group 1.

The javadoc on the Pattern class covers the regular expression syntax.

If you are looking for a pattern that might recur some number of times, you can use Matcher.find() repeatedly until it returns false. Matcher.group(0) once on each iteration will then return what matched that time.

If you want to build one big regular expression that matches everything all at once (which I believe is what you want) then around each of the three sets of things that you want to capture, put a set of capturing parenthesis, use Matcher.match() and then Matcher.group(n) where n is 1, 2 and 3 respectively. Of course Matcher.match() might also return false, in which case the pattern did not match, and you can't retrieve any of the groups.

In your example, what you probably want to do is have it match some preceding text, then start a capturing group, match for digits, end the capturing group, etc...I don't know enough about your exact input format, but here is an example.

Lets say I had strings of the form:

Eat 12 carrots at 12:30
Take 3 pills at 01:15

And I wanted to extract the quantity and times. My regular expression would look something like:

"\w+ (\d+) [\w ]+ (\d{1,2}:\d{2})"

The code would look something like:

Pattern p = Pattern.compile("\\w+ (\\d+) [\\w ]+ (\\d{2}:\\d{2})");
Matcher m = p.matcher(oneline);
if(m.matches()) {
    System.out.println("The quantity is " + m.group(1));
    System.out.println("The time is " + m.group(2));
}

The regular expression means "a string containing a word, a space, one or more digits (which are captured in group 1), a space, a set of words and spaces ending with a space, followed by a time (captured in group 2, and the time assumes that hour is always 0-padded out to 2 digits). I would give a closer example to what you are looking for, but the description of the possible input is a little vague.

🌐
Alvin Alexander
alvinalexander.com › blog › post › java › how-extract-group-string-regex-pattern-matcher-find
Java: How to extract a group from a String that contains a regex pattern | alvinalexander.com
import java.util.regex.Matcher; import java.util.regex.Pattern; public class PatternMatcherGroup1 { public static void main(String[] args) { String stringToSearch = "Four score and seven years ago our fathers ..."; Pattern p = Pattern.compile(" (\\S+or\\S+) "); // the pattern to search for Matcher m = p.matcher(stringToSearch); // if we find a match, get the group if (m.find()) { // we're only looking for one group, so get it String theGroup = m.group(1); // print the group out for verification System.out.format("'%s'\n", theGroup); } } }
🌐
Stack Overflow
stackoverflow.com › questions › 11105579 › extract-string-using-regex-in-java
extract string using regex in java - Stack Overflow
Ok I updated the entry, I expect to take out the two strings out of the < > . ... Well, giving us one sample does not say what variance there can be in the format. So let's assume that it's really rigid: the two fields are always surrounded by "Batch job ... success" and separated by spaces. Then a regex might look like this:
🌐
Coding-stream-of-consciousness
coding-stream-of-consciousness.com › 2019 › 04 › 22 › java-regex-capture-extract-multiple-values
Java Regex Capture/Extract Multiple Values | Coding Stream of Consciousness
April 22, 2019 - import java.util.regex.Matcher; import java.util.regex.Pattern; private static final String capturePattern = "^/.*/SXF_SX_(\\d+)_(\\d{4}-\\d{2}-\\d{2}.\\d{2}.\\d{2}.\\d{2}.\\d{3}).log:(.*) INFO.*" + "copy job (.*) for the dataset:.*" //Leaving out rest of class, this is just the regex parsing portion.