Java Program to Extract a Single Quote Enclosed String From a Larger String using Regex Last Updated : 31 Jan, 2023 Comments Improve Suggest changes Like Article Like Report Problem Statement: Given a String extract the substring enclosed in single quotes (') using Java Regex. Java regex is an API for pattern matching with regular expression. 'java.util.regex' is a class used for CharSequence< interface in order to support matching against characters from a wide variety of input sources. It is not allowed to pass a null argument to any classes or interfaces as it will throw out an exception called <. Hence, it can be concluded 'java.util.regex' is containing Interface and classes as follows: ClassesMatcher: A engine that performs match operations on a character sequence by interpreting a Pattern.Pattern: A compiled representation of a regular expression. Illustration: 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 Example Java // 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 Geeks' only"; // Desired custom output String string2 = "The data that I want is'Java Regex'"; // Paranthesis indicate it is a group and signifies // it can have substring enclosed in single quote Pattern p = Pattern.compile(".*'([^']*)'.*"); // This method returns a pattern object // Calling matcher() method of pattern object // and passing input character sequence Matcher m1 = p.matcher(string1); Matcher m2 = p.matcher(string2); // Printing complete entered string 1 System.out.println("String to be extracted : " + string1); // Condition check using matches() method which // looks out for content if any in single quote if (m1.matches()) { // Print the required sub-string System.out.println("Extracted part : " + m1.group(1)); } // New line System.out.println(); // Printing complete entered string 2 System.out.println("String to be extracted : " + string2); // Condition check using matches() method which // looks out for content if any in single quote if (m2.matches()) { // Print the required sub-string System.out.println("Extracted part : " + m2.group(1)); } } } OutputString to be extracted : Out of this String I want 'Geeks for Geeks' only Extracted part : Geeks for Geeks String to be extracted : The data that I want is'Java Regex' Extracted part : Java Regex Time complexity : O(n), where n is the length of the input strings "string1" and "string2". Space complexity :O(1), as the space used by the program remains constant regardless of the size of the input strings. Comment More infoAdvertise with us Next Article Java Program to Extract a Single Quote Enclosed String From a Larger String using Regex U uvrajanshuman Follow Improve Article Tags : Java Technical Scripter Java Programs Technical Scripter 2020 java-regular-expression Java-String-Programs +2 More Practice Tags : Java Similar Reads Java Program to Extract an HTML Tag from a String using RegEx In this article, we will find/extract an HTML tag from a string with help of regular expressions. The Regular Expression Regex or Rational Expression is simply a character sequence that specifies a search pattern in a particular text. It can contain a single character or it can have a complex sequen 3 min read Java Program to Convert String to String Array Using Regular Expression Regular Expressions or Regex (in short) is an API for defining String patterns that can be used for searching, manipulating, and editing a string in Java. Email validation and passwords are few areas of strings where Regex is widely used to define the constraints. Regular Expressions are provided un 3 min read Java Program to Search a Particular Word in a String Using Regex In Java string manipulation, searching for specific words is a fundamental task. Regular expressions (regex) offer a powerful and flexible approach to achieve this search. It matches the patterns simply by comparing substrings. In this article, we will learn how to search for a particular word in a 3 min read Extracting IP Address From a Given String Using Regular Expressions Prerequisites: Regular ExpressionsIP addressGiven a string str, the task is to extract all the IP addresses from the given string. Let us see how to extract IP addresses from a file. Input: str="The IPV4 address of Port A is: 257.120.223.13. And the IPV6 address is:fffe:3465:efab:23fe:2235:6565:aaab 4 min read How to Extract a Specific Line from a Multi-Line String in Java? In Java, String Plays an important role. As String stores or we can say it is a collection of characters. We want to get a specific line from a multi-line String in Java. This can also be done in Java. In this article, we will be learning how to extract a specific line from a multi-line String in Ja 2 min read How to Remove All Punctuation from a String using Regex in Java? In this article, we will explain how to remove all punctuation from a given String by using Java with the help of Regex. Here regex means regular expression. The regex is a powerful way to explain the search pattern. One more thing is that regular expressions are mostly used for Searching a required 4 min read How to Replace the First Occurrence of a String Using Regex in Java? Regex is a very interesting way to search for patterns in a String that the user provides. Regex stands for Regular Expressions. It consists of some patterns that can be planned and modified according to the usage of the program. In this article, we will discuss how to replace the first occurrence o 2 min read Capitalize the First Letter of a String in Java Using Regex In Java, we can use regular expressions to make the first character of each word in Uppercase as the word is represented as a String and string is the collection of characters. So, in Java, we can use pattern and matcher class methods to find the first character of a word and then replace it with it 2 min read How to Convert a Java String Against a Pattern Regex? Regular expressions, or "regex," are an effective tool in Java programming for checking texts against a given pattern. This post will walk you through the process of utilizing regular expressions to validate a Java text against a pattern. Prerequisites:String in JavaRegular Expressions in JavaConver 2 min read How to Remove Duplicates from a String in Java Using Regex ? In Java programming, Regex (regular expressions) is a very important mechanism. It can be used to match patterns in strings. Regular expressions provide a short and simple syntax for describing the patterns of the text. In this article, we will be learning how to remove duplicates from a String in J 2 min read Like