How to Implement Regex in Java


In this example we will show how to implement regex using Pattern and Matcher classes in Java

Source Code

package com.beginner.examples;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
public class RegexExample {
	
    public static void main(String a[]){
    	// regex rule
    	Pattern p = Pattern.compile("d{2}$");
        Matcher m = p.matcher("1345");
        System.out.println("Is '1345' ends with min 2 digits? " + m.find());
    }
}

Output:

Is '1345' ends with min 2 digits? true

References

Imported packages in Java documentation:

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments