Documente Academic
Documente Profesional
Documente Cultură
Example
We can write a regular expression to represent all valid mail ids & by using that regular
expression we can validate whether the given mail id is valid or not.
We can write a regular expression to represent to all valid java identifiers.
The main application areas of regular expressions are
o We can implement validation mechanism.
o We can develop pattern matching applications
o We can develop translators like compilers & interpreters etc.
o We can use for designing digital circuits.
o We can use to develop communication protocols like TCP IP, UDP etc.
Program Example
Import java.util.regex.Matcher;
Import java.util.regex.Pattern;
Output:
0----2----ab
5----7----ab
7----9----ab
11----13----ab
Pattern Class:
A pattern object represents compiled version of regular expression. We can create a pattern object by
using compile () method of Pattern class.
A matcher object can be used to match character sequence against a regular expression. We can create
a matcher object by using matcher() method of Pattern class.
Character class:
Example:
importjava.util.regex.Matcher;
importjava.util.regex.Pattern;
publicclassDemoReg {
publicstaticvoid main(String[] args) {
Pattern p=Pattern.compile("[ab]"); //x
Matcher m=p.matcher("a3b@c4z#");
while(m.find())
{
System.out.println(m.start()+"--------"+m.group());
}
}
}
Output:
x=[ab]
0--------a
2--------b
x=[a-z]
0--------a
2--------b
4--------c
6--------z
x=[0-9]
1--------3
5--------4
x=[0-9a-z]
0--------a
1--------3
2--------b
4--------c
5--------4
6--------z
Example:
importjava.util.regex.Matcher;
importjava.util.regex.Pattern;
publicclassDemoReg {
publicstaticvoid main(String[] args) {
Pattern p=Pattern.compile("[\\d]");\\ x
Matcher m=p.matcher("a3z4@ k7z#");
while(m.find())
{
System.out.println(m.start()+"--------"+m.group());
}
}
Output:
x =\\d
1--------3
3--------4
7--------7
x=\\w
0--------a
1--------3
2--------z
3--------4
6--------k
7--------7
8--------z
x=\\s
5--------
x=.
0--------a
1--------3
2--------z
3--------4
4--------@
5--------
6--------k
7--------7
8--------z
9--------#
Quantifiers:
Example
Program Example
importjava.util.regex.*;
publicclassDemoReg {
publicstaticvoid main(String[] args) {
Pattern p=Pattern.compile("a");
Matcher m=p.matcher("abaabaaab");// x
while(m.find())
{
System.out.println(m.start()+"--------"+m.group());
}
}
Output:
x=a
0--------a
2--------a
3--------a
5--------a
6--------a
7--------a
x=a+
0--------a
2--------aa
5--------aaa
x=a*
0--------a
1--------
2--------aa
4--------
5--------aaa
8--------
9--------
x=a
0--------a
1--------
2--------a
3--------a
4--------
5--------a
6--------a
7--------a
8--------
9--------
Spilt Method:
Pattern class contains split() method to spilt given String according to a regular expression
Example
importjava.util.regex.*;
publicclassDemoReg {
publicstaticvoid main(String[] args) {
Pattern p=Pattern.compile("\\s"); /* x \\. or [.] */
String s[]=p.split("This is an expression program."); /* www.gooogle.com */
for(String s1: s)
{
System.out.println(s1);
}
}
Output:
x=\\s
This
is
an
expression
program.
x=\\.
www
google
com
String class also contains spilt to spilt the given String against a regular expression.
Example-
publicclassDemoReg {
publicstaticvoid main(String[] args) {
String ss="www.google.com";
String s[]=ss.split("\\.");
for(String s1: s)
{
System.out.println(s1);
}
}
}
Output:
www
google
com
Note-
Pattern class sptit() can take target String as argument whereas String class split can take regular
expression as argument.
StringTokenizer:
We can use StringTokenizer to divide the target String into stream of tokens according to default
regular expression or delimiter.
StringTokenizer class presenting in java.util. Package.
Example1
importjava.util.StringTokenizer;
publicclassDemoStringTokenizer {
publicstaticvoid main(String[] args) {
StringTokenizerst=newStringTokenizer("This is a demo program");
while(st.hasMoreTokens())
{
System.out.println(st.nextToken());
}
}
}
Output:
This
is
a
demo
program
Example2-
importjava.util.StringTokenizer;
publicclassDemoStringTokenizer {
publicstaticvoid main(String[] args) {
StringTokenizerst=newStringTokenizer("1,234,000",",");
while(st.hasMoreTokens())
{
System.out.println(st.nextToken());
}
}
Output: -
1
234
000
1: Write a regular expression the set all valid identifiers in java language
Rules:
Rules:
Rules