Skip to content
Related Articles

Related Articles

Improve Article

Java Program to Extract a Single Quote Enclosed String From a Larger String using Regex

  • Difficulty Level : Easy
  • Last Updated : 05 Oct, 2021

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:

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

  • Interface
    1. MatchResult: The result of a match operation
  • Classes
    1. Matcher: A engine that performs match operations on a character sequence by interpreting a Pattern.
    2. 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));
        }
    }
}
Output
String 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



My Personal Notes arrow_drop_up
Recommended Articles
Page :