Regular Expression by Durga Sir
Regular Expression by Durga Sir
Example1:
package RegularExcpression;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
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;
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)
}
}
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.
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.
Matcher m = p.matcher("abbabbba");
package RegularExcpression;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
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)
}
Output:
0 a
2 b
The total number of occurance is: 2
. Any character
package RegularExcpression;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
while(m.find()) {
count++;
System.out.println(m.start() + " " + " " + m.group());
//start index, end index and which group is matched(ab)
}
Output:
3
The total number of occurance is: 1
Quantifiers:
We can use quantifiers to specify number of occurrences to match.
import java.util.regex.Matcher;
import java.util.regex.Pattern;
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)
}
}
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:
package RegularExcpression;
import java.util.regex.Pattern;
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;
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;
for(String s1 : s) {
System.out.println(s1);
}
}
}
Output:
durga s
ftware s
luti
n
package RegularExcpression;
import java.util.regex.Pattern;
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;
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;
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;
while (st.hasMoreTokens()) {
System.out.println(st.nextToken());
}
}
}
Output:
durga
software
solution
package RegularExcpression;
import java.util.StringTokenizer;
Output:
20
12
2022
Rules:
[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/91)?[7-9][0-9]{9}
Mail-id:
S123_xzs.k@gmail.com
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;
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");
}
}
package RegularExcpression;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
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");
}
}
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;
while(line != null) {
Matcher m = p.matcher(line);
while(m.find()) {
pw.println(m.group());
}
br.readLine();
}
br.close();
pw.flush();
pw.close();
}
}
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;
}
Write a program to display all .txt file present in E:\\TypingMaster:
package RegularExcpression;
import java.io.*;
import java.util.regex.*;
package RegularExcpression;
import java.io.*;
import java.util.regex.*;
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