Regular Expression


Regular Expression (Video Here )
regular expressionregex or regexp[1] (sometimes called a rational expression)[2][3] is a sequence of characters that define a search pattern. Usually such patterns are used by string searching algorithms for "find" or "find and replace" operations on strings, or for input validation. It is a technique developed in theoretical computer science and formal language theory.
The concept arose in the 1950s when the American mathematician Stephen Cole Kleene formalized the description of a regular language. The concept came into common use with Unix text-processing utilities. Different syntaxes for writing regular expressions have existed since the 1980s, one being the POSIX standard and another, widely used, being the Perl syntax.
Regular expressions are used in search engines, search and replace dialogs of word processors and text editors, in text processing utilities such as sed and AWK and in lexical analysis. Many programming languages provide regex capabilities either built-in or via libraries.
Quantification
quantifier after a token (such as a character) or group specifies how often that a preceding element is allowed to occur. The most common quantifiers are the question mark ?, the asterisk * (derived from the Kleene star), and the plus sign + (Kleene plus).
?The question mark indicates zero or one occurrences of the preceding element. For example, colou?r matches both "color" and "colour".
*The asterisk indicates zero or more occurrences of the preceding element. For example, ab*c matches "ac", "abc", "abbc", "abbbc", and so on.
+The plus sign indicates one or more occurrences of the preceding element. For example, ab+c matches "abc", "abbc", "abbbc", and so on, but not "ac".
{n}[19]The preceding item is matched exactly n times.
{min,}[19]The preceding item is matched min or more times.
{min,max}[19]The preceding item is matched at least min times, but not more than max times.
Regular Expression can be recursively defined as follows −
  • ε is a Regular Expression indicates the language containing an empty string. (L (ε) = {ε})
  • φ is a Regular Expression denoting an empty language. (L (φ) = { })
  • x is a Regular Expression where L = {x}
  • If X is a Regular Expression denoting the language L(X) and Y is a Regular Expression denoting the language L(Y), then
    • X + Y is a Regular Expression corresponding to the language L(X) ∪ L(Y) where L(X+Y) = L(X) ∪ L(Y).
    • X . Y is a Regular Expression corresponding to the language L(X) . L(Y) where L(X.Y) = L(X) . L(Y)
    • R* is a Regular Expression corresponding to the language L(R*)where L(R*) = (L(R))*
  • If we apply any of the rules several times from 1 to 5, they are Regular Expressions.

Some RE Examples

Regular ExpressionsRegular Set
(0 + 10*)L = { 0, 1, 10, 100, 1000, 10000, … }
(0*10*)L = {1, 01, 10, 010, 0010, …}
(0 + ε)(1 + ε)L = {ε, 0, 1, 01}
(a+b)*Set of strings of a’s and b’s of any length including the null string. So L = { ε, a, b, aa , ab , bb , ba, aaa…….}
(a+b)*abbSet of strings of a’s and b’s ending with the string abb. So L = {abb, aabb, babb, aaabb, ababb, …………..}
(11)*Set consisting of even number of 1’s including empty string, So L= {ε, 11, 1111, 111111, ……….}
(aa)*(bb)*bSet of strings consisting of even number of a’s followed by odd number of b’s , so L = {b, aab, aabbb, aabbbbb, aaaab, aaaabbb, …………..}
(aa + ab + ba + bb)*String of a’s and b’s of even length can be obtained by concatenating any combination of the strings aa, ab, ba and bb including null, so L = {aa, ab, ba, bb, aaab, aaba, …………..}

Comments

Post a Comment

Popular posts from this blog

Compiler Construction (Conversion of NFA to DFA)

Compiler Construction (Lectures during COVID-19 Quarantine days)

Intro to Programming (MCS) & PF (BSSE) (Topic: Recursion & Iteration in C++)