Regular Expression
If we want to represent a group of strings according to a
particular pattern then we should go for regular expression.
Example1:
We can write a regular expression to represent all valid mobile
numbers.
We can write a regular expression to represent all mail id's.
The main important application areas of regular expression are
1. To develop validation frameworks.
2. To develop pattern matching applications (Ctrl + F in windows and
grip command in unix).
3. To develop translators like assemblers, compilers, interpreters
etc.
4. To develop digital circuits.
5. To develop communication protocols like TCP/IP, UDP etc.
package RegularExcpression;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegExDemo {
public static void main(String[] args) {
int count = 0;
Pattern p = Pattern.compile("ab");
Matcher m = p.matcher("abbabbba"); // Mathcer class present
in Pattern class
while(m.find()) {
count++;
System.out.println(m.start()); //start index
}
System.out.println("The total number of occurance is: " +
count);
}
Output:
0
3
The total number of occurance is: 2
package RegularExcpression;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegExDemo {
public static void main(String[] args) {
int count = 0;
Pattern p = Pattern.compile("ab");
Matcher m = p.matcher("abbabbba"); // Mathcer class present
in Pattern class
while(m.find()) {
count++;
System.out.println(m.start() + " " + m.end() + " " +
m.group()); //start index, end index and which group is matched(ab)
}
System.out.println("The total number of occurance is: " +
count);
}
}
Output:
0 2 ab
3 5 ab
The total number of occurance is: 2
Pattern:
A pattern object is a compiled version of regular expression, i.e
it is a java equivalent object of pattern.
We can create a pattern object by using compile() method of
pattern class.
Public static Pattern compile(String re);
Pattern p = Pattern.compile("ab");
Matcher:
We can use Matcher object to check the given pattern in the
target String.
We can create a Matcher object by using matcher() method of
pattern class.
Public Matcher matcher(String target);
Matcher m = p.matcher("abbabbba");
Important methods of Matcher class:
boolean find();---it attempts to find next match and returns
true, if it is available.
int start();----return start index of match.
int end();----return end+1 index of the match.
String group();---it returns the matched pattern.
Note: Pattern and Matcher classes present in java.util.regex package
and introduced in java 1.4 version.
Character classes:
[abc] either 'a' or 'b' or 'c'
[^abc] except 'a' and 'b' and 'c'
[a-z] any lower case alphabet symbol
from a to z.
[A-Z] any upper case alphabet symbol
from A to Z.
[a-zA-Z] any alphabet symbol.
[0-9] any digit from 0 to 9.
[0-9a-zA-Z] any alphanumeric symbol.
[^0-9a-zA-Z] except alphanumeric symbol.
package RegularExcpression;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegExDemo {
public static void main(String[] args) {
int count = 0;
Pattern p = Pattern.compile("[abc]");
Matcher m = p.matcher("a3b#k@9z"); // Mathcer class present
in Pattern class
while(m.find()) {
count++;
System.out.println(m.start() + " " + " " + m.group());
//start index, end index and which group is matched(ab)
}
System.out.println("The total number of occurance is: " +
count);
}
}
Output:
0 a
2 b
The total number of occurance is: 2
[abc] [^abc] [a-z] [0-9] [a-zA-Z0-9] [^a-zA-Z0-9]
0 a 1 3 0 a 1 3 0 a 3 #
2 b 3 # 2 b 6 9 1 3 5 @
4 k 4 k 2 6
5 @ 7 z 4 k
6 9 6 9
7 z 7 z
Predefined character classes:
\s Space character.
\S Except space character
\d Any digit from 0 to 9 [0-9]
\D Except digit, any character
\w Any word character [0-9a-zA-Z]]
\W Except word character (special
character)
. Any character
package RegularExcpression;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegExDemo {
public static void main(String[] args) {
int count = 0;
Pattern p = Pattern.compile("\\s");
Matcher m = p.matcher("a7b #k@9z"); // Mathcer class present
in Pattern class
while(m.find()) {
count++;
System.out.println(m.start() + " " + " " + m.group());
//start index, end index and which group is matched(ab)
}
System.out.println("The total number of occurance is: " +
count);
}
Output:
3
The total number of occurance is: 1
\\s \\S \\d \\D \\w \\W .
3 0 a 1 7 0 a 0 a 3 . . 0 a
1 7 6 9 2 b 1 7 5 @ 1 7
2 b 3…. 2 b 2 b
4 k 4 k 4 k 3 . .
5 @ 5 @ 6 9 4 k
6 9 7 z 7 z 5 @
7 z 6 9
7 z
Quantifiers:
We can use quantifiers to specify number of occurrences to match.
a Exactly one 'a'.
a+ Atleast one 'a'.
a* Any number of a's including zero.
a? Atmost one 'a'.
package RegularExcpression;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegExDemo {
public static void main(String[] args) {
int count = 0;
Pattern p = Pattern.compile("a");
Matcher m = p.matcher("abaabaab"); // Mathcer class present
in Pattern class
while(m.find()) {
count++;
System.out.println(m.start() + " " + " " + m.group());
//start index, end index and which group is matched(ab)
}
System.out.println("The total number of occurance is: " +
count);
}
}
Output:
0 a
2 a
3 a
5 a
6 a
The total number of occurance is: 5
'a' 'a+' 'a*' 'a?'
0 a 0 a 0 a 0 a
2 a 2 aa 1 . . 1 . .
3 a 5 aaa 2 aa 2 a
5 a 4 . . 3 a
6 a 5 aaa 4 . .
7 a 8 . . 5 a
9 . . 6 a
7 a
8 . .
9 . .
Pattern class split() method:
We can use Pattern class split() method to split the target
String according to a particular pattern.
package RegularExcpression;
import java.util.regex.Pattern;
public class RegExDemo {
public static void main(String[] args) {
Pattern p = Pattern.compile("\\s");
String[] s = p.split("durga software solution");
for(String s1 : s) {
System.out.println(s1);
}
}
Output:
durga
software
solution
package RegularExcpression;
import java.util.regex.Pattern;
public class RegExDemo {
public static void main(String[] args) {
Pattern p = Pattern.compile("a");
String[] s = p.split("durga software solution");
for(String s1 : s) {
System.out.println(s1);
}
}
}
Output:
durg
softw
re solution
package RegularExcpression;
import java.util.regex.Pattern;
public class RegExDemo {
public static void main(String[] args) {
Pattern p = Pattern.compile("o"); //o aate hi split ho
jaayega 'o' print nhi hoga baki sb print hoga including space
String[] s = p.split("durga software solution");
for(String s1 : s) {
System.out.println(s1);
}
}
}
Output:
durga s
ftware s
luti
n
package RegularExcpression;
import java.util.regex.Pattern;
public class RegExDemo {
public static void main(String[] args) {
Pattern p = Pattern.compile("\\.");
String[] s = p.split("www.durgasoftware.com");
for(String s1 : s) {
System.out.println(s1);
}
}
}
Output:
www
durgasoftware
com
package RegularExcpression;
import java.util.regex.Pattern;
public class RegExDemo {
public static void main(String[] args) {
Pattern p = Pattern.compile("[.]");
String[] s = p.split("www.durgasoftware.com");
for(String s1 : s) {
System.out.println(s1);
}
}
}
Output:
www
durgasoftware
com
String class also contains split method to split the target
string according to a particular pattern.
package RegularExcpression;
import java.util.regex.Pattern;
public class RegExDemo {
public static void main(String[] args) {
String s = new String("durga software solution");
String[] s1 = s.split("\\s");
for(String s2 : s1) {
System.out.println(s2);
}
}
}
Output:
durga
software
solution
Note:
Pattern class split() method can take target string as argument, where
as string class split() method can take pattern as argument.
String Tokenizer:
It is a specially designed class for tokenization activity.
String tokenizer present in java.util package.
package RegularExcpression;
import java.util.StringTokenizer;
public class RegExDemo {
public static void main(String[] args) {
StringTokenizer st = new StringTokenizer("durga software
solution");
while (st.hasMoreTokens()) {
System.out.println(st.nextToken());
}
}
}
Output:
durga
software
solution
The default regular expression for String Tokenizer class is
space.
package RegularExcpression;
import java.util.StringTokenizer;
public class RegExDemo {
public static void main(String[] args) {
StringTokenizer st = new StringTokenizer("20-12-2022", "-");
while (st.hasMoreTokens()) { // target string pattern
System.out.println(st.nextToken());
}
}
Output:
20
12
2022
Write a regular expression to represent all valid ten digit mobile
numbers.
Rules:
Every number should contain exactly 10 digits.
The 1st digit should 7 or 8 or 9.
[789][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]
Or
[7-9][0-9]{9}
10 digits/ 11 digits:
0?[7-9][0-9]{9} ((?) symbol means we can take 0 in count or ignore it)
10 digits/ 11 digits/ 12 digits:
(0/91)?[7-9][0-9]{9}
Mail-id:
[email protected]
Regular expression:
[a-zA-Z0-9][a-zA-Z0-9_.]*@[a-zA-Z0-9]+([.][a-zA-Z]+)+
Write a program to check whether the given mobile number is valid or
not.
package RegularExcpression;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegExDemo {
public static void main(String[] args) {
Pattern p = Pattern.compile("(0/91)[7-9][0-9]{9}");
Matcher m = p.matcher(args[0]);
if (m.find() && m.group().equals(args[0])) {
System.out.println("Valid mobile number");
} else {
System.out.println("Invalid mobile number");
}
}
Write a program to check whether the given mail-id is valid or not.
Change the mobile number regular expression with mail-id regular
expression.
package RegularExcpression;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegExDemo {
public static void main(String[] args) {
Pattern p = Pattern.compile("[a-zA-Z0-9][a-zA-Z0-9_.]*@[a-
zA-Z0-9]+([.][a-zA-Z]+)+");
Matcher m = p.matcher(args[0]);
if (m.find() && m.group().equals(args[0])) {
System.out.println("Valid mail-id");
} else {
System.out.println("Invalid mail-id");
}
}
Write a program to read all mobile number present in given 'input.txt'
file where mobile numbers are mixed with normal data.
package RegularExcpression;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegExDemo {
public static void main(String[] args) throws IOException {
PrintWriter pw = new PrintWriter("Output1.txt");
Pattern p = Pattern.compile("(0|91)?[7-9][0-9]{9}");
BufferedReader br = new BufferedReader(new
FileReader("input.txt"));
String line = br.readLine();
while(line != null) {
Matcher m = p.matcher(line);
while(m.find()) {
pw.println(m.group());
}
br.readLine();
}
br.close();
pw.flush();
pw.close();
}
}
Write a program to read all mail-id's present in given 'input.txt'
file where mobile numbers are mixed with normal data.
package RegularExcpression;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegExDemo {
public static void main(String[] args) throws IOException {
PrintWriter pw = new PrintWriter("Output1.txt");
Pattern p = Pattern.compile("[a-zA-Z0-9][a-zA-Z0-9_.]*@[a-
zA-Z0-9]+([.][a-zA-Z]+)+");
BufferedReader br = new BufferedReader(new
FileReader("input.txt"));
String line = br.readLine();
while (line != null) {
Matcher m = p.matcher(line);
while (m.find()) {
pw.println(m.group());
}
br.readLine();
}
br.close();
pw.flush();
pw.close();
}
Write a program to display all .txt file present in E:\\TypingMaster:
package RegularExcpression;
import java.io.*;
import java.util.regex.*;
public class RegExDemo {
public static void main(String[] args) throws IOException {
Pattern p = Pattern.compile("[a-zA-Z0-9_.$]+[.]txt");
File f = new File("E:\\TypingMaster");
String[] s = f.list();
int count = 0;
for (String s1 : s) {
Matcher m = p.matcher(s1);
if (m.find() && m.group().equals(s1)) {
count++;
System.out.println(s1);
}
}
System.out.println("The total number: " + count);
}
Write a program to display txt/gif file file present in E:\\
TypingMaster:
package RegularExcpression;
import java.io.*;
import java.util.regex.*;
public class RegExDemo {
public static void main(String[] args) throws IOException {
Pattern p = Pattern.compile("[a-zA-Z0-9_.$]+[.](txt|gif)");
File f = new File("E:\\TypingMaster");
String[] s = f.list();
int count = 0;
for (String s1 : s) {
Matcher m = p.matcher(s1);
if (m.find() && m.group().equals(s1)) {
count++;
System.out.println(s1);
}
}
System.out.println("The total number: " + count);
}
Output:
basic.gif
Bubbles.gif
checkmark.gif
entersymbol.gif
ergo.gif
msgbullet.gif
readme.txt
Results.gif
spacer.gif
Statistics.gif
tmteam.gif
The total number: 11