CodesJava

Easy learning with example program codes

Java Regular expressions regex tutorial


Regular expressions

Regular expressions represents a sequence of symbols and characters expressing a string or pattern to be searched for within a longer piece of text. The abbreviation for regular expression is regex. In programming regular expressions are mainly used to define constraint on strings like password, email validation.

The java.util.regex package primarily consists of the following 1 interface and 3 classes:

  1. MatchResult interface
  2. Pattern class
  3. Matcher class
  4. PatternSyntaxException class

Pattern class

Pattern class is used to define a pattern for the regex. A pattern class object represents a compiled version of a regular expression.

Pattern class methods

Method Description
static Pattern compile(String regex) It compiles the given regex and return the instance of pattern.
Matcher matcher(CharSequence input) It creates a matcher that matches the given input with pattern.
static boolean matches(String regex, CharSequence input) It works as the combination of compile and matcher methods. It compiles the regular expression and matches the given input with the pattern.
String[] split(CharSequence input) It splits the given input string around matches of given pattern.
String pattern() It returns the regex pattern.

Matcher class

Matcher class object is the regex engine and used to perform match operations on a character sequence.

Matcher class methods

Method Description
boolean matches() It test whether the regular expression matches the pattern.
boolean find() It finds the next expression that matches the pattern.
boolean find(int start) It finds the next expression that matches the pattern from the given start number.
String group() It returns the matched subsequence.
int start() It returns the starting index of the matched subsequence.
int end() It returns the ending index of the matched subsequence.
int groupCount() It returns the total number of the matched subsequence.

PatternSyntaxException class

PatternSyntaxException class object represents an unchecked exception that refers to a syntax error in a regular expression pattern.

Regular Expression Syntax

Subexpression Matches
          ^ Matches the beginning of the line.
          $ Matches the end of the line.
          . Matches any single character except newline. Using m option allows it to match the newline as well.
          […] Matches any single character in brackets.
         [^…] Matches any single character not in brackets.
         A Beginning of the entire string.
         z End of the entire string.
         Z End of the entire string except allowable final line terminator.
         re* Matches 0 or more occurrences of the preceding expression.
         re+ Matches 1 or more of the previous thing.
        re? Matches 0 or 1 occurrence of the preceding expression.
       re{ n} Matches exactly n number of occurrences of the preceding expression.
      re{ n,} Matches n or more occurrences of the preceding expression.
      re{ n, m} Matches at least n and at most m occurrences of the preceding expression.
        a| b Matches either a or b.
        (re) Groups regular expressions and remembers the matched text.
        (?: re) Groups regular expressions without remembering the matched text.
         (?> re) Matches the independent pattern without backtracking.
           w Matches the word characters.
           W Matches the nonword characters.
            s Matches the whitespace. Equivalent to [tnrf].
            S Matches the nonwhitespace.
           d Matches the digits. Equivalent to [0-9].
           D Matches the nondigits.
          A Matches the beginning of the string.
           Z Matches the end of the string. If a newline exists, it matches just before newline.
           z Matches the end of the string.
          G Matches the point where the last match finished.
          n Back-reference to capture group number “n”.
          b Matches the word boundaries when outside the brackets. Matches the backspace (0x08) when inside the brackets.
          B Matches the nonword boundaries.
      n, t, etc. Matches newlines, carriage returns, tabs, etc.
           Q Escape (quote) all characters up to E.
          E Ends quoting begun with Q.

Java Regular Expressions Example

We can write a regular expression in 3 ways. Let us discuss these with the below example.

package com.codesjava;
 
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
public class RegexTest {
	public static void main(String args[]){
		//1st way  
		//. represents single character  
		Pattern p = Pattern.compile(".s");
		Matcher m = p.matcher("js");  
		boolean boolean1 = m.matches();  
		System.out.println(boolean1);   
		//2nd way  
		boolean boolean2=Pattern.compile(".s").matcher("js").matches();  
		System.out.println(boolean2);   
		//3rd way  
		boolean boolean3 = Pattern.matches(".s", "js");  
		System.out.println(boolean3);   
	}
}

Output

true
true
true

Java Regex Character classes

Character Class Description
[abc] a, b, or c (simple class)
[^abc] Any character except a, b, or c (negation)
[a-zA-Z] a through z or A through Z, inclusive (range)
[a-d[m-p]] a through d, or m through p: [a-dm-p] (union)
[a-z&&[def]] d, e, or f (intersection)
[a-z&&[^bc]] a through z, except for b and c: [ad-z] (subtraction)
[a-z&&[^m-p]] a through z, and not m through p: [a-lq-z](subtraction)

Regex Character Class Example

package com.codesjava;
 
import java.util.regex.Pattern;
 
public class RegexTest {
	public static void main(String args[]){
		//true (2nd char is j)
		System.out.println(Pattern.matches(".j", "pj"));  
		//false (2nd char is not s)  
		System.out.println(Pattern.matches(".j", "ab"));
		//false (s is not the last char)  
		System.out.println(Pattern.matches(".s", "msa"));
		//false (has more than 2 char before s)  
		System.out.println(Pattern.matches(".s", "pjms"));
		//true (3rd char is s)   
		System.out.println(Pattern.matches("..s", "pjs"));
	}
}

Output

true
false
false
false
true

Java Regex Quantifiers

The quantifiers specify how often an element can occur. The symbols ?, *, + and {} define the quantity of the regular expressions.
 

Regex Description
X? X occurs once or not at all
X+ X occurs once or more times
X* X occurs zero or more times
X{n} X occurs n times only
X{n,} X occurs n or more times
X{y,z} X occurs at least y times but less than z times

Java Regex Quantifiers Example

package com.codesjava;
 
import java.util.regex.Pattern;
 
public class RegexTest {
	public static void main(String args[]){
		System.out.println("? quantifier ....");  
		//true (a or j or n comes one time) 
		System.out.println(Pattern.matches("[ajn]?", "a")); 
		//false (a comes more than one time) 
		System.out.println(Pattern.matches("[ajn]?", "aaa")); 
		//false (a j and n comes more than one time)
		System.out.println(Pattern.matches("[ajn]?", "aammmnn"));  
		//false (a comes more than one time)
		System.out.println(Pattern.matches("[ajn]?", "aazzta"));  
		//false (a or j or n must come one time) 
		System.out.println(Pattern.matches("[ajn]?", "aj")); 
 
		System.out.println("+ quantifier ....");  
		//true (a or j or n once or more times)  
		System.out.println(Pattern.matches("[ajn]+", "a"));
		//true (a comes more than one time) 
		System.out.println(Pattern.matches("[ajn]+", "aaa")); 
		//true (a or j or n comes more than once)  
		System.out.println(Pattern.matches("[ajn]+", "aammmnn"));
		//false (z and t are not matching pattern)
		System.out.println(Pattern.matches("[ajn]+", "aazzta"));  
 
		System.out.println("* quantifier ....");  
		//true (a or j or n may come zero or more times) 
		System.out.println(Pattern.matches("[ajn]*", "ajjjna")); 
	}
}

Output

? quantifier ....
true
false
false
false
false
+ quantifier ....
true
true
false
false
* quantifier ....
true

Java regex metacharacters

Regex Description
. Any character (may or may not match terminator)
d Any digits, short of [0-9]
D Any non-digit, short for [^0-9]
s Any whitespace character, short for [tnx0Bfr]
S Any non-whitespace character, short for [^s]
w Any word character, short for [a-zA-Z_0-9]
W Any non-word character, short for [^w]
b A word boundary
B A non-word boundary

Regex Metacharacters Example

package com.codesjava;
 
import java.util.regex.Pattern;
 
public class RegexTest {
	public static void main(String args[]){
		//\d means digit
		System.out.println("metacharacters d....");  
 
		//false (non-digit)  
		System.out.println(Pattern.matches("\d", "jai"));
		//true (digit and comes once)
		System.out.println(Pattern.matches("\d", "5"));  
		//false (digit but comes more than once)  
		System.out.println(Pattern.matches("\d", "3343"));
		//false (digit and char)
		System.out.println(Pattern.matches("\d", "323abc"));  
 
		//\D means non-digit 
		System.out.println("metacharacters D....");  
		//false (non-digit but comes more than once)  
		System.out.println(Pattern.matches("\D", "jai"));
		//false (digit)  
		System.out.println(Pattern.matches("\D", "5"));
		//false (digit) 
		System.out.println(Pattern.matches("\D", "3343")); 
		//false (digit and char)  
		System.out.println(Pattern.matches("\D", "323abc"));
		//true (non-digit and comes once)
		System.out.println(Pattern.matches("\D", "j"));  
 
		System.out.println("metacharacters D with quantifier....");  
		//true (non-digit and may come 0 or more times)
		System.out.println(Pattern.matches("\D*", "jai")); 
	}
}

Output

true
metacharacters d....
false
true
false
false
metacharacters D....
false
false
false
false
true
metacharacters D with quantifier....
true

Java regular expression examples

Sign Up/ Sign In
Ask a Question


Industrial Training

We offers Placement Oriented Training on Java, Spring, JSF, Hibernate, PHP, AngularJS, Angular 4, PLSQL, Oracle BI Publisher etc. We also provides Online training, please mail us at hr@codesjava.com.

Development

We also provides the Development services for Website Development , Java Development, PHP Development, Android App Development etc. You can contact us on hr@codesjava.com.

Copyright © 2018 CodesJava DMCA.com Protection Status SiteMap Reference: Java Wiki