Introduction

The main application areas of regular expressions are developing pattern matching applications like Compilers,Interpreters,Text editors etc.

Example:

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegExDemo {

	public static void main(String[] args) {
	
		Pattern p=Pattern.compile("ba");
		Matcher m=p.matcher("ababbaabbbabc");
		while(m.find()){
			System.out.println(m.start());
		}
	}

}
/*
Output:
      1
      4
      9
*/

Pattern class

A Pattern object represents compiled version of regular expression.We can create a Pattern object by using compile method of Pattern class

public static Pattern compile(String regex)

Matcher class

Matcher class used to match character sequences against a given regular expression.We can create a Mather object by using matcher() method of Pattern class.

public Matcher matcher(String charSequence)

Note: Pattern and Matcher classes are available in java.util.regex package.

Method Name Method Description
public boolean find() It attempts to find next match,
if it is available it returns true otherwise returns false
public int start() Returns start index of match.
public int end() Returns end index of match.
public String group() Returns the matching string.

Character classes

Character class Description
[a] a
[abc] a or b or c.
[a-zA-Z] Any alphabet symbol.
[a-zA-Z0-9] Any alpha numeric symbol.
[^abc] Except a or b or c.

Example:1

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegExDemo {

	public static void main(String[] args) {
	
		Pattern p=Pattern.compile("[a-z]");
		Matcher m=p.matcher("0ab1cd32x23c");
		while(m.find()){
			System.out.println(m.start()+"---"+m.group());
		}
	}

}
/*
Output:
1---a
2---b
4---c
5---d
8---x
11---c
*/

Example:2

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegExDemo {

	public static void main(String[] args) {
	
		Pattern p=Pattern.compile("[0-9]");
		Matcher m=p.matcher("0ab1cd32x23c");
		while(m.find()){
			System.out.println(m.start()+"---"+m.group());
		}
	}

}
/*
Output:
0---0
3---1
6---3
7---2
9---2
10---3
*/

Example:3

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegExDemo {

	public static void main(String[] args) {
	
		Pattern p=Pattern.compile("[^abc]");
		Matcher m=p.matcher("0ab1cd32x23c");
		while(m.find()){
			System.out.println(m.start()+"---"+m.group());
		}
	}

}
/*
Output:
0---0
3---1
5---d
6---3
7---2
8---x
9---2
10---3
*/

Predefined Character classes

Character class Description
\s Empty space character
\d Any digit from 0 to 9.
\w Any word character.[a-zA-Z0-9]
. Any character(Word character + Special character).

Example:1

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegExDemo {

	public static void main(String[] args) {
	
		Pattern p=Pattern.compile("\\s");
		Matcher m=p.matcher("0a3#b 6e 7d");
		while(m.find()){
			System.out.println(m.start()+"---"+m.group());
		}
	}
}
/*
Output:
5--- 
8--- 
*/

Example:2

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegExDemo {

	public static void main(String[] args) {
	
		Pattern p=Pattern.compile("\\d");
		Matcher m=p.matcher("0a3#b 6e 7d");
		while(m.find()){
			System.out.println(m.start()+"---"+m.group());
		}
	}
}
/*
Output:
0---0
2---3
6---6
9---7
*/

Example:3

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegExDemo {

	public static void main(String[] args) {
	
		Pattern p=Pattern.compile("\\w");
		Matcher m=p.matcher("0a3#b 6e 7d");
		while(m.find()){
			System.out.println(m.start()+"---"+m.group());
		}
	}
}
/*
Output:
0---0
1---a
2---3
4---b
6---6
7---e
9---7
10---d
*/
<strong>Example:4</strong>
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegExDemo {

	public static void main(String[] args) {
	
		Pattern p=Pattern.compile(".");
		Matcher m=p.matcher("0a3#b 6e 7d");
		while(m.find()){
			System.out.println(m.start()+"---"+m.group());
		}
	}
}
/*
Output:
0---0
1---a
2---3
3---#
4---b
5--- 
6---6
7---e
8--- 
9---7
10---d
*/

Quantifiers

Quantifiers are allowed to specify number of occurrences of characters.

Quantifier Description
a A single a
a* Zero or more a’s
a+ At least one a
a? At most one a.i.e either zero or one a

Example:1

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegExDemo {

	public static void main(String[] args) {
	
		Pattern p=Pattern.compile("a");
		Matcher m=p.matcher("abaabbaaab");
		while(m.find()){
			System.out.println(m.start()+"---"+m.group());
		}
	}

}
/*
Output:
0---a
2---a
3---a
6---a
7---a
8---a
*/

Example:2

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegExDemo {

	public static void main(String[] args) {
	
		Pattern p=Pattern.compile("a+");
		Matcher m=p.matcher("abaabbaaab");
		while(m.find()){
			System.out.println(m.start()+"---"+m.group());
		}
	}

}
/*
Output:
0---a
2---aa
6---aaa
*/

Exmaple:3

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegExDemo {

	public static void main(String[] args) {
	
		Pattern p=Pattern.compile("a*");
		Matcher m=p.matcher("abaabbaaab");
		while(m.find()){
			System.out.println(m.start()+"---"+m.group());
		}
	}

}
/*
Output:
0---a
1---
2---aa
4---
5---
6---aaa
9---
10---
*/

Example:4

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegExDemo {

	public static void main(String[] args) {
	
		Pattern p=Pattern.compile("a?");
		Matcher m=p.matcher("abaabbaaab");
		while(m.find()){
			System.out.println(m.start()+"---"+m.group());
		}
	}

}
/*
Output:
0---a
1---
2---a
3---a
4---
5---
6---a
7---a
8---a
9---
10---
*/
Difference between Pattern class split method and String class split method

Pattern class split method:
Example:1

import java.util.regex.Pattern;

public class RegExDemo {

	public static void main(String[] args) {
	
		Pattern p=Pattern.compile("\\s");
		String[] s=p.split("Oracle Corporation");
		for(String s1:s){
			System.out.println(s1);
		}
	}
}
/*
Output:
Oracle
Corporation
*/

Example:2

import java.util.regex.Pattern;
public class RegExDemo {

	public static void main(String[] args) {
	
		Pattern p=Pattern.compile("\\.");
		String[] s=p.split("www.java2learn.com");
		for(String s1:s){
			System.out.println(s1);
		}
	}
}

/*
Output:
www
java2learn
com
*/
String class split method:

public class RegExDemo {

	public static void main(String[] args) {

		String s = "www.java2learn.com";
		String[] s1 = s.split("\\.");
		for (String s2 : s1) {
			System.out.println(s2);
		}
	}
}
/*
Output;
www
java2learn
com

*/