Thursday, July 4, 2024
HomeLanguagesJavaPattern compile(String) method in Java with Examples

Pattern compile(String) method in Java with Examples

The compile(String) method of the Pattern class in Java is used to create a pattern from the regular expression passed as parameter to method. Whenever you need to match a text against a regular expression pattern more than one time, create a Pattern instance using the Pattern.compile() method.
Syntax: 

public static Pattern compile(String regex)

Parameters: This method accepts one single parameter regex which represents the given regular expression compiled into a pattern.
Return Value: This method returns the pattern compiled from the regex passed to the method as a parameter.
Exception: This method throws following exception:  

  • PatternSyntaxException: This exception is thrown if the expression’s syntax is invalid.

Below programs illustrate the compile(String) method: 
Program 1:  

Java




// Java program to demonstrate
// Pattern.compile() method
 
import java.util.regex.*;
 
public class GFG {
    public static void main(String[] args)
    {
        // create a REGEX String
        String REGEX = ".*www.*";
 
        // create the string
        // in which you want to search
        String actualString
            = "www.neveropen.co.za";
 
        // compile the regex to create pattern
        // using compile() method
        Pattern pattern = Pattern.compile(REGEX);
 
        // get a matcher object from pattern
        Matcher matcher = pattern.matcher(actualString);
 
        // check whether Regex string is
        // found in actualString or not
        boolean matches = matcher.matches();
 
        System.out.println("actualString "
                           + "contains REGEX = "
                           + matches);
    }
}


Output: 

actualString contains REGEX = true

 

Program 2: 

Java




// Java program to demonstrate
// Pattern.compile method
 
import java.util.regex.*;
 
public class GFG {
    public static void main(String[] args)
    {
        // create a REGEX String
        String REGEX = "brave";
 
        // create the string
        // in which you want to search
        String actualString
            = "Cat is cute";
 
        // compile the regex to create pattern
        // using compile() method
        Pattern pattern = Pattern.compile(REGEX);
 
        // check whether Regex string is
        // found in actualString or not
        boolean matches = pattern
                              .matcher(actualString)
                              .matches();
 
        System.out.println("actualString "
                           + "contains REGEX = "
                           + matches);
    }
}


Output: 

actualString contains REGEX = false

 

Reference: https://docs.oracle.com/javase/10/docs/api/java/util/regex/Pattern.html#compile(java.lang.String)

Nokonwaba Nkukhwana
Experience as a skilled Java developer and proven expertise in using tools and technical developments to drive improvements throughout a entire software development life cycle. I have extensive industry and full life cycle experience in a java based environment, along with exceptional analytical, design and problem solving capabilities combined with excellent communication skills and ability to work alongside teams to define and refine new functionality. Currently working in springboot projects(microservices). Considering the fact that change is good, I am always keen to new challenges and growth to sharpen my skills.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments