Regex by 131357

10

Click here to load reader

Transcript of Regex by 131357

Page 1: Regex by 131357

REGULAR EXPRESSIONS

Junaid LodhiRoll # 131357Air University Pakistan

Page 2: Regex by 131357

WHY TO STUDY REGULAR EXPRESSION?

Todays’s Agenda

Page 3: Regex by 131357

WHAT IS REGULAR EXPRESSION? regular expressions are used to generate patterns of strings. A regular expression is an algebraic formula whose value is a pattern consisting of a set of strings, called the language of the expression. This regular expression describes Canadian postal codes /^[A-Z][0-9][A-Z] [0-9][A-Z][0-9]$/

Any language accepted by the Finite Automata can be represented by Regular Expression

Page 4: Regex by 131357

OPERATORS USED IN REGULAR EXPRESSIONSI. Union: If R1 and R2 are regular expressions, then R1 | R2 (also

written as R1 U R2 or R1 + R2) is also a regular expression.L(R1|R2) = L(R1) U L(R2).

II. Concatenation: If R1 and R2 are regular expressions, then R1R2 (also written as R1.R2) is also a regular expression.L(R1R2) = L(R1) concatenated with L(R2).

III. Kleene closure: If R1 is a regular expression, then R1* (the Kleene closure of R1) is also a regular expression.L(R1*) = epsilon U L(R1) U L(R1R1) U L(R1R1R1) U ...

NOTE:Closure has the highest precedence, followed by concatenation, followed by union.

Page 5: Regex by 131357

EXAMPLES OF REGULAR EXPRESSION

The set of strings over {A..Z,a..z} that contain the word "main".Let <letter> = A | B | ... | Z | a | b | ... | z <letter>* main <letter>*

Page 6: Regex by 131357

EXAMPLES CONTINUED Set of EVEN Length String Regular expression can be made as

Page 7: Regex by 131357

EXAMPLES CONTINUED Set of ODD Length String Regular expression can be made as

Page 8: Regex by 131357

EXAMPLE CONTINUED Set of Strings Divisible by 3 Regular expression can be made as

Page 9: Regex by 131357

PRACTICE QUESTION Congruent to 2 mod 3 What can be the Regular Expression?

Page 10: Regex by 131357