Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
Java Pattern pattern() Method
Next article icon

Java Pattern pattern() Method

Last Updated : 10 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

pattern() method of the Pattern class in Java is used to get the regular expression which is compiled to create this pattern. We use a regular expression to create the pattern and this method is used to get the same source expression. 

Example 1: Using the pattern() method to check the regex pattern passed for pattern matching.

Java
// Java program to demonstrate // Pattern.pattern() method import java.util.regex.*;  public class Geeks  {     public static void main(String[] args)     {         // create a REGEX String         String REGEX = "(.*)(for)(.*)?";          // create the string in which you want       	// to search         String actualString = "code of Machine";          // create pattern         Pattern pattern1 = Pattern.compile(REGEX);          // find the regular expression of pattern         String RegularExpression = pattern1.pattern();          System.out.println("Pattern's RegularExpression = "                            + RegularExpression);     } } 

Output
Pattern's RegularExpression = (.*)(for)(.*)? 

Syntax

public String pattern()

  • Parameters: This method does not accepts anything as parameter. 
  • Return Value: This method returns the pattern's source regular expression.  

Example 2: Using the pattern() method in conjunction with a Matcher.

Java
// Java program to demonstrate the usage of  // regular expressions in pattern matching import java.util.regex.Matcher; import java.util.regex.Pattern;  public class Geeks  {       public static void main(String[] args)     {         // Input strings to match the regex against         String input1 = "The quick brown fox jumps over the lazy dog";         String input2 = "The quick red fox jumps over the lazy dog";                  // Regex pattern to match case-insensitive 'the'         String regex = "(?i)the";      	         // Compile the regex pattern         Pattern pattern = Pattern.compile(regex);                  // Create matchers for both input strings         Matcher matcher1 = pattern.matcher(input1);         Matcher matcher2 = pattern.matcher(input2);                  // Find and print all matches in input1         while (matcher1.find()) {             System.out.println("Match 1: " + matcher1.group());         }                  // Find and print all matches in input2         while (matcher2.find()) {             System.out.println("Match 2: " + matcher2.group());         }     } } 

Output
Match 1: The Match 1: the Match 2: The Match 2: the 

Next Article
Java Pattern pattern() Method

A

AmanSingh2210
Improve
Article Tags :
  • Java
  • Java-Functions
  • Java 8
  • Java-Pattern
Practice Tags :
  • Java

Similar Reads

    Java Matcher pattern() Method
    The pattern() method of the Matcher class is used to get the pattern to be matched by this matcher.Example 1: The below example demonstrates how the pattern() method retrieves the regex pattern "G.*s$" used to match a string ending with "s" and starting with "G".Java// Java code to illustrate patter
    2 min read
    Pattern toString() Method in Java with Examples
    toString() method of a Pattern class used to return the string representation of this pattern. This return the regular expression from which this pattern was compiled. Syntax: public String toString() Parameters: This method accepts nothing as parameter. Return value: This method returns a string re
    1 min read
    Matcher usePattern(Pattern) method in Java with Examples
    The usePattern() method of Matcher Class is used to get the pattern to be matched by this matcher. Syntax: public Matcher usePattern(Pattern newPattern) Parameters: This method takes a parameter newPattern which is the new pattern to be set. Return Value: This method returns a Matcher with the new P
    2 min read
    Pattern quote(String) method in Java with Examples
    quote(String) method of a Pattern class used to returns a literal pattern String for the specified String passed as parameter to method.This method produces a String equivalent to s that can be used to create a Pattern. Metacharacters or escape sequences in the input sequence will be given no specia
    3 min read
    Pattern splitAsStream() Method in Java with Examples
    splitAsStream() method of a Pattern class used to return a stream of String from the given input sequence passed as parameter around matches of this pattern.this method is the same as the method split except that what we get back is not an array of String objects but a stream. If this pattern does n
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences