Java Regex


The Regular expression is an API which defines a pattern for searching strings. Abbreviation for regular expression is called regex.

 

The search pattern is everything from a character, and a complex expression containing special characters that tell the pattern. The pattern that defined by regex may match one or several times or not at all for a given string. Regex is used to search, edit and manipulate text.

 

The process of modifying a text with a regex is to the string or text. The pattern is applied to the text from left to right. Once source character has been used in a match, it cannot be reused.

 

The regex is mainly used to define the constraint on strings such as email validation and password.


Java.util.regex package

Classes are for matching character sequences against patterns specified by regular expressions. This package provides classes and interfaces as following:

  • MatchResult interface:

    It is the result of a match operation.

  • Matcher class:

    It is an engine that performs match operations on a character sequence by interpreting a pattern.

  • Pattern class:

    It is compiled representation of a regular expression and used to define a pattern for regex engine.

  • PatternSyntaxException class:

    It is an unchecked exception thrown that indicate a syntax error in a regular expression pattern.


Matcher class

No Method Description
1 boolean matches() To check the regular expression matches the pattern.
2 boolean find() To finds the next expression which matches the pattern.
3 boolean find(int start) To finds the next expression which matches the pattern with the help of given start number.
4 String group() it returns the matched subsequence.
5 int groupCount() it returns the total number of the matched subsequence.

Pattern class

No Method Description
1 Matcher matcher(CharSequence input) It creates a matcher that matches the given input with the pattern.
2 static Pattern compile(String regex It compiles the given regex and returns the instance of the Pattern.
3 static boolean matches(String regex, CharSequence input) It compiles the regular expression and matches the given input with the pattern.
4 String pattern() It returns the regex pattern.
5 String[] split(CharSequence input) It splits the given input string with matches of given pattern.

Example of Java Regex:

The Java regex can write in 3 ways:

1st way

import java.util.regex.*;  

public class Regex1{  
	public static void main(String args[]){  
		Pattern p = Pattern.compile(".e");
        //dot represents single character  
		Matcher m = p.matcher("be");  
		boolean ba = m.matches();  
  		System.out.println(ba);
	}   
} 

Output

true  

2nd way

import java.util.regex.*;  

public class Regex2{  
	public static void main(String args[]){  
		boolean ba=Pattern.compile(".e").matcher("be").matches();  
    	System.out.println(ba);
	}   
} 

Output

true 

3rd way

import java.util.regex.*;  

public class Regex3{  
	public static void main(String args[]){  
		boolean ba = Pattern.matches(".e", "be");  
		System.out.println(ba);
	}   
} 

Output

true 

Example

The dot represents a single character.

import java.util.regex.*;  

public class RegexExample{  
	public static void main(String args[]){  
		System.out.println(Pattern.matches(".e", "be"));
        //true  
		System.out.println(Pattern.matches(".e", "as"));
        //false  
		System.out.println(Pattern.matches(".e", "bet"));
        //false (It has more than 2 char)    
		System.out.println(Pattern.matches("..e", "tie"));
        //true (3rd char is e)  
	}
}   



Onlinetpoint is optimized for basic learning, practice and more. Examples are well checked and working examples available on this website but we can't give assurity for 100% correctness of all the content. This site under copyright content belongs to Onlinetpoint. You agree to have read and accepted our terms of use, cookie and privacy policy.