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. This is quite easy to do using split: ... Since the split method accepts a regex ...
🌐
W3docs
w3docs.com › home › code snippets › java › how to extract a substring using regex
How to extract a substring using regex | W3docs
To extract a substring from a string using a regular expression in Java, you can use the following steps: Compile the regular expression pattern using the compile() method of the Pattern class: Pattern pattern = Pattern.compile("regex pattern"); Copy · Create a Matcher object from the input ...
🌐
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:
🌐
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 - Almost all of the string splitting methods in Java uses whitespace character as the default delimiter. In this section, we will use the Scanner class to extract text based on particular delimiters, or based on a pattern. Very often, we use the scanner class to get the input from users via a console user interface by specifying the System input as its constructor parameter.
🌐
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 - From the above input string we are going to print the extracted substring 1361167373I09PN1098350476TKA9I which is of 30 character length using Matcher and Pattern utility in Java. I have posted a separate article on How to generate Unique code of 'n' characters lenght using Random util in Java. For extracting the Unique Code from the string below is the Regex pattern used:
🌐
javaspring
javaspring.net › blog › java-substring-regex
Mastering Java Substring Regex: A Comprehensive Guide — javaspring.net
In Java, the java.util.regex package provides classes for working with regex. The main classes are Pattern and Matcher. Pattern: This class represents a compiled regular expression. It is used to define the pattern that we want to match. Matcher: This class is used to perform the matching operation against an input string. It provides methods to find matches, extract groups, and more. public class SubstringExample { public static void main(String[] args) { String original = "Hello, World!"; // Extract substring starting from index 7 String sub1 = original.substring(7); System.out.println(sub1); // Extract substring from index 0 to 5 (exclusive) String sub2 = original.substring(0, 5); System.out.println(sub2); } }
Find elsewhere
🌐
Iditect
iditect.com › faq › java › how-to-extract-a-substring-using-regex-in-java.html
How to extract a substring using regex in java
import java.util.regex.Matcher; import java.util.regex.Pattern; public class RegexSubstringExample { public static void main(String[] args) { // Input string String input = "The price of the product is $25.99 and the discount is 10%."; // Define the regular expression pattern String regex = "\\$\\d+(\\.\\d+)?"; // Matches currency values like $25.99 // Create a Pattern object Pattern pattern = Pattern.compile(regex); // Create a Matcher object Matcher matcher = pattern.matcher(input); // Find the first match if (matcher.find()) { // Extract the matched substring String matchedSubstring = matcher.group(); System.out.println("Matched substring: " + matchedSubstring); } else { System.out.println("No match found."); } } }
🌐
Stack Overflow
stackoverflow.com › questions › 43680885 › how-to-use-extract-substring-using-regex
java - How to use extract substring using regex - Stack Overflow
April 28, 2017 - I modified the regex from the top answer to suit your case. I replace the ^…$ (line start/end) portion with \b (word boundaries). import java.util.regex.*; public class RegexTest { public static final String PACKAGE_PATTERN = "\\b[a-z][a-z0-9_]*(\\.[a-z0-9_]+)+[0-9a-z_]\\b"; public static void main(String[] args) { String s = "id= com.mycomp.war.tasks.JmxMetricsTask I run/id-geLh3hM1-1_2 [Svc--DAG]"; Pattern p = Pattern.compile(PACKAGE_PATTERN, Pattern.DOTALL | Pattern.CASE_INSENSITIVE); Matcher m = p.matcher(s); if (m.find()) { System.out.println(m.group()); // com.mycomp.war.tasks.JmxMetricsTask } } }
🌐
Coderanch
coderanch.com › t › 590292 › java › Regex-extract-substring
Regex to extract a substring (Java in General forum at Coderanch)
Chandra shekar M wrote:So i want to define a regex in such a way that i should be able to extract everything after http:// and out put should be [email protected] and 8080. How can i do this? Just remove the brackets from the first expression. However, I wonder why you think you need to do this.
🌐
Stack Overflow
stackoverflow.com › questions › 16316562
regex - How to extract the sub string in Java - Stack Overflow
Do anybody have good way to extract the the 3 strings: (update [page1] [desc]) ([desc] is not null) (Rule:setvalue; SourceValue:[ign [desc]]; TargetField:[desc2]) the 3 part may be any characters. Java regex is also fine to implement.
🌐
Regex101
regex101.com › r › fRSw5i › 1
regex101: Extract substring from a string
Regular expression tester with syntax highlighting, explanation, cheat sheet for PHP/PCRE, Python, GO, JavaScript, Java, C#/.NET, Rust.