Regular Expression – Part 2

Regular Expression


Character Classes
[abc] – either a , b or c
[^abc] – except a, b, c
[a-z] – any lower case alphabet symbol from a to z
[A-Z] – any lower case alphabet symbol from A to Z
[0-9] – Any digit from 0 to 9
[a-zA-Z] = Any alphabet
[0-9] – Any digit from 0 to 9
[0-9a-zA-Z] = Any alphanumeric symbols
[^0-9a-zA-Z] = Special symbol except alphanumeric symbols

Pre-defined Character Classes
\s – Space character
\S – Expect 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

Quantifiers – We can use quantifiers to specify the number of occurrences to match
a = Exactly one ‘a’
a+ = atleast one ‘a’
a* = Any number a’s including zero number
a? = Almost one ‘a’

Java Example –

Java Regular Expression – Part 1

Watch here:

Regular Expression

1. If we want to represent a group of a string according to a particular pattern then we should go for Regular Expression.
e.g. We can write a regular expression to represent all valid mobiles.
e.g. We can write a regular expression to represent all email ids.
2. The main important application areas of regular expressions are a. To develop validation frameworks e.g. email
b. To develop pattern matching applications (control+f in windows, grep in Unix)
c. To develop translators like assemblers, compilers, interpreters etc
Compiler design phases
2.a Lexical Analysis -> Syntax Analysis -> Semantic Analysis -> Intermediate Code Generation (I.C.G) -> Code optimisation (CO) -> Target Code Generation
Lexical Analysis = Tokenization (Scanner). (Implemented based on RE)
Syntax – Parsing
d. To develop digital circuits.
e. To develop communication protocols like TCP/IP, UDP etc

3. A Pattern object is a compiled version of a regular expression, that is, it is a java equivalent object of the pattern. We can create pattern object using compile method of pattern class.
4. We can use Matcher object to check the given pattern in the target string. We can matcher object using matcher method of Pattern class.

Contact us at