Java Regular Expressions

  1. Syntax
  2. Simple Expression(Match) in Java
  3. Start & End Methods in Java

Here I am using,
OS : Linux (Ubuntu 12.04)
IDE : Eclipse Tool
Eclipse : Juno (Version 4.2.0)
Package : Default package

A. Syntax

Regular Expressions

Sub ExpressionMatches
^Matches beginning of line.
$Matches end of line.
.Matches any single character except newline. Using m option allows it to match newline as well.
[...]Matches any single character in brackets.
[^...]Matches any single character not in brackets
\ABeginning of entire string
\zEnd of entire string
\ZEnd of entire string except allowable final line terminator.
re*Matches 0 or more occurrences of preceding expression.
re+Matches 1 or more of the previous thing
re?Matches 0 or 1 occurrence of preceding expression.
re{ n}Matches exactly n number of occurrences of preceding expression.
re{ n,}Matches n or more occurrences of preceding expression.
re{ n, m}Matches at least n and at most m occurrences of preceding expression.
a| bMatches either a or b.
(re)Groups regular expressions and remembers matched text.
(?: re)Groups regular expressions without remembering matched text.
(?> re)Matches independent pattern without backtracking.
\wMatches word characters.
\WMatches nonword characters.
\sMatches whitespace. Equivalent to [\t\n\r\f].
\SMatches nonwhitespace.
\dMatches digits. Equivalent to [0-9].
\DMatches nondigits.
\AMatches beginning of string.
\ZMatches end of string. If a newline exists, it matches just before newline.
\zMatches end of string.
\GMatches point where last match finished.
\nBack-reference to capture group number "n"
\bMatches word boundaries when outside brackets. Matches backspace (0x08) when inside brackets.
\BMatches nonword boundaries.
\n, \t, etc.Matches newlines, carriage returns, tabs, etc.
\QEscape (quote) all characters up to \E
\EEnds quoting begun with \Q

B. Simple Expression(Match) in Java

public class Regular_Ex1 {
	void check(String fname, String lname) {
		String match = null;
		if(fname(fname) == true) 
			match = "Accepted";
			match = "Not Accepted";
		System.out.println("First Name " + match);
		if(lname(lname) == true)
			match = "Accepted";
			match = "Not Accepted";
		System.out.println("Last Name " + match);
	private boolean fname(String fname) {
		// TODO Auto-generated method stub
		return fname.matches("[A-Z][a-zA-Z]*");
	private boolean lname(String lname) {
		// TODO Auto-generated method stub
		return lname.matches("[a-zA-z]+([ '-][a-zA-Z]+)*");
class MainClass {
	public static void main(String args[]) {
		Regular_Ex1 obj = new Regular_Ex1();
		obj.check("Balaji", "G");
		System.out.println("\nWrong Entry..\n");
		obj.check("45", "Abs**klmn");

Sample Output

First Name Accepted
Last Name Accepted

Wrong Entry..

First Name Not Accepted
Last Name Not Accepted

C. Start & End Methods in Java

import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Regular_Ex2 {
	private static final String REG = "\\bcode\\b";
    private static final String INPUT = "code code samplecodez code";
	void check() {
		Pattern p = Pattern.compile(REG);
		Matcher m = p.matcher(INPUT); // get a matcher object
		int count = 0;
		while(m.find()) {
			System.out.println("Match Number : " + count);
			System.out.println("Starting Point : " + m.start());
			System.out.println("Ending Point : " + m.end() + "\n");
class MainClass {
    public static void main( String args[] ){
    	Regular_Ex2 obj = new Regular_Ex2();

Sample Output

Match Number : 1
Starting Point : 0
Ending Point : 4

Match Number : 2
Starting Point : 5
Ending Point : 9

Match Number : 3
Starting Point : 22
Ending Point : 26


product 2

product 3

Feedbacks :