Java Pattern pattern() Method Last Updated : 11 Jul, 2025 Comments Improve Suggest changes 13 Likes 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); } } OutputPattern's RegularExpression = (.*)(for)(.*)? Syntaxpublic 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()); } } } OutputMatch 1: The Match 1: the Match 2: The Match 2: the Create Quiz Comment A AmanSingh2210 Follow 13 Improve A AmanSingh2210 Follow 13 Improve Article Tags : Java Java-Functions Java 8 Java-Pattern Explore Java BasicsIntroduction to Java3 min readJava Programming Basics9 min readJava Methods6 min readAccess Modifiers in Java4 min readArrays in Java7 min readJava Strings7 min readRegular Expressions in Java3 min readOOP & InterfacesClasses and Objects in Java5 min readAccess Modifiers in Java4 min readJava Constructors4 min readJava OOP(Object Oriented Programming) Concepts10 min readJava Packages2 min readJava Interface7 min readCollectionsCollections in Java12 min readCollections Class in Java13 min readCollection Interface in Java4 min readIterator in Java4 min readJava Comparator Interface5 min readException HandlingJava Exception Handling6 min readJava Try Catch Block4 min readJava final, finally and finalize4 min readChained Exceptions in Java3 min readNull Pointer Exception in Java5 min readException Handling with Method Overriding in Java4 min readJava AdvancedJava Multithreading Tutorial3 min readSynchronization in Java7 min readFile Handling in Java4 min readJava Method References7 min readJava 8 Stream Tutorial7 min readJava Networking6 min readJDBC Tutorial5 min readJava Memory Management3 min readGarbage Collection in Java6 min readMemory Leaks in Java3 min readPractice JavaJava Interview Questions and Answers1 min readJava Programs - Java Programming Examples7 min readJava Exercises - Basic to Advanced Java Practice Programs with Solutions5 min readJava Quiz1 min readJava Project Ideas For Beginners and Advanced15+ min read Like