---
title: "Java Regex or Regular Expression in java"  
description: "Regular Expression used to define constraint on strings such as password and email validation."  
author: "Anupam Mishra"  
published: 2015-11-09  
updated: 2018-03-27  
canonical: https://www.mindstick.com/articles/11909/java-regex-or-regular-expression-in-java  
category: "java"  
tags: ["java", "j2ee", "regex"]  
reading_time: 2 minutes  

---

# Java Regex or Regular Expression in java

**The [Regular Expression](https://www.mindstick.com/forum/65/regular-expression) is an API** to define [pattern](https://www.mindstick.com/forum/2314/what-is-the-mvp-and-mvc-pattern) for searching or manipulating strings.\

**Java Regex API provides 1 [interface](https://www.mindstick.com/articles/12036/interface-in-c-sharp) and 3 classes in java.util.regex [package](https://www.mindstick.com/blog/12619/tips-for-finding-the-right-package-when-you-buy-instagram-followers) i.e.**

1. MatchResult interface

2. Matcher class

3. Pattern class

4. PatternSyntaxException class

The Matcher and Pattern classes are widely used in java regular expression.

## Matcher Class:

\

It implements MatchResult interface. It is a regex engine i.e. used to perform

match [operations](https://www.mindstick.com/news/2675/bmw-agrees-to-integrate-blockchain-with-operations-partners-with-bnb-chain-coinweb) on a [character](https://www.mindstick.com/articles/23551/an-investigate-distinctive-seafood-restaurant-for-your-image-stamp-character) sequence.

## Pattern Class:

It is the compiled [version](https://www.mindstick.com/articles/12845/things-to-remember-while-migrating-odoo-to-a-better-version) of a regular expression. It is used to define a pattern for the regex engine.

For Example, we see the [implementation](https://answers.mindstick.com/qa/44913/who-is-the-minister-of-statistics-and-programme-implementation) of java regex with the help of following example

> ```
> import java.util.regex.*;  public class RegexEx{  public static void main(String
> args[]){  //First way
> Pattern p =
> Pattern.compile(".s");//compiles the given regex and return the
> instance of pattern.Pattern q =
> Pattern.compile("a.");//. represents single character in Regular
> Expression  Matcher m = p.matcher("as");
> //creates a matcher that matches the given input with pattern. boolean b = m.matches();/*It compiles the
> regular expression and matches the given input with the pattern.  */   //Second
> way   boolean b2=Pattern.matches("[amn]",
> "a");//true (among a or m or n)
>   //Third way
> boolean b3 =
> Pattern.matches("[amn]", "a");  boolean b4 =
> Pattern.matches("a.", "as");System.out.println(b+"
> "+b2+" "+b3+" "+b4);// another operations System.out.println("metacharacters
> d...."); //d means digit  System.out.println(Pattern.matches("\\d",
> "abc"));//false (non-digit)  System.out.println(Pattern.matches("\\d",
> "1")); //true (digit and comes once)
>   System.out.println("metacharacters
> D....");//D means non-digit    System.out.println(Pattern.matches("\\D",
> "abc"));//false (non-digit but comes more than once) System.out.println(Pattern.matches("\\D",
> "m"));//true (non-digit and comes once) System.out.println("metacharacters D
> with quantifier....");  System.out.println(Pattern.matches("\\D*",
> "mak"));//true (non-digit and may come 0 or more times)   }}
> ```

**The output of the above [program](https://www.mindstick.com/blog/12337/scaling-up-your-mentorship-program) is shown below:** **\**

---

Original Source: https://www.mindstick.com/articles/11909/java-regex-or-regular-expression-in-java

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
