Sunteți pe pagina 1din 8

Regular Expression

 Any Group of String according to a particular Pattern is called “Regular Expression”.

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;

Public class DemoReg {


publicstaticvoid main(String[] args) {
Pattern p=Pattern.compile("ab");
Matcher m=p.matcher("abbbbababcdab");
while(m.find())
{
System.out.println(m.start()+"----"+m.end()+"----"+m.group());
}
}

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.

Pattern p=Pattern.compile(String regularExpression);


Matcher 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.

Matcher m=p.matcher(String target);

Important methods of matcher class:

(1) boolean find ()


It attempts to find next match & if it is available returns true otherwise returns false.
(2) Int start()
returns start index of the match.
(3) Int end()
renturn end index of the match.
(4) String group()
returns the matched pattern

Character class:

(1) [a-z] ----------- Any lower case alphabet symbol


(2) [A-Z] ---------- Any upper case alphabet symbol
(3) [a-zA-Z] ------ Any alphabet symbol
(4) [0-9] ----- Any digit from 0 to 9
(5) [abc] ----- Either a or b or c
(6) [^abc] ---- Except a or b or c
(7) [0-9a-zA-Z] ---- Any alpha numeric character.

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

Predefined Character Class:

 Space character --------- \s


 [0-9] ------------ \d
 [0-9a-zA-Z} ------ \w
 Any character ------ .

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:

 We can use quantifiers to specify no of characters to match

Example

A. a --------------------------- Exactly one a


B. a+ --------------------------at least one a
C. a* -------------------------- Any no a is
D. a? ------------------------- at most one a

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 split () method

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

Note: - default regular expression is space.

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

Regular Expression Examples

1: Write a regular expression the set all valid identifiers in java language

Rules:

A. The length of each identifier is at least two.


B. The allowed characters are a to z or A to Z or 0 to 9.
C. The first character should not digit.

2:Write a regular expression to represent all valid mobile numbers

Rules:

A. Mobile number contains 10 digits


B. The first digit should be 7 to 9

3:Write a regular expression to represent all valid mail ids

Rules

A. The set of allowed characters in mail are 0 to 9, a-z & A-Z.


B. Should starts with alphabet symbol
C. Should contains at least one symbol

S-ar putea să vă placă și