Theory of Computation & Computer Graphics β Introduction to Finite Automata, NEC licence examination syllabus (Nepal Engineering Council).
Regular Expressions
An algebraic notation for exactly the languages finite automata can recognise.
π Where this lives: Regular expressions escaped computer science entirely. They validate the email field on every signup form, filter logs, drive find-and-replace in every editor, and are the first tool any programmer reaches for when text needs pattern matching. Ken Thompson put them into the Unix editor ed in 1968, from which the command "global / regular expression / print" gave us grep β a program name that is literally an abbreviation of a regular expression operation. Search "Ken Thompson regular expression grep history unix".
Definition and algebra
A REGULAR EXPRESSION IS A PATTERN THAT DESCRIBES A SET OF
STRINGS. The definition is INDUCTIVE β three base cases and three
constructors, and nothing else.
ββ THE BASE CASES ββββββββββββββββββββββββββββββββββββββββββ
β denotes the EMPTY LANGUAGE { }, containing no
strings at all
Ξ΅ denotes the language { Ξ΅ }, containing exactly ONE
string: the empty string
a for each a β Ξ£, denotes { a }
NOTE THE DISTINCTION between β and Ξ΅, which is a standard
confusion: β CONTAINS NOTHING; { Ξ΅ } CONTAINS ONE THING that
happens to have length zero. An empty box and a box containing
an empty bag are different.
ββ THE OPERATORS, in increasing precedence βββββββββββββββββ
UNION r + s (or r | s) L(r) βͺ L(s)
CONCATENATION r s { xy : x β L(r),
y β L(s) }
KLEENE STAR r* zero or more copies of r
concatenated
PRECEDENCE: STAR binds tightest, then CONCATENATION, then
UNION. So
ab* + c means (a(b*)) + c
and not a(b*+c) or (ab)*+c. Parentheses override this.
KLEENE STAR ALWAYS INCLUDES Ξ΅, since "zero copies" is
permitted: L(r*) = {Ξ΅} βͺ L(r) βͺ L(rr) βͺ β¦
THE POSITIVE CLOSURE rβΊ = rr* means ONE or more, excluding Ξ΅
unless Ξ΅ β L(r).
ββ THE ALGEBRAIC IDENTITIES ββββββββββββββββββββββββββββββββ
These are used constantly in simplification questions:
r + s = s + r union is COMMUTATIVE
(r + s) + t = r + (s + t) union is associative
r + r = r union is IDEMPOTENT
r + β = r β is the union identity
(rs)t = r(st) concatenation associative
Ξ΅r = rΞ΅ = r Ξ΅ is the concatenation
identity
β r = rβ = β β ANNIHILATES
r(s + t) = rs + rt distributive
(s + t)r = sr + tr
(r*)* = r* star is IDEMPOTENT
Ξ΅* = β * = Ξ΅
r* = Ξ΅ + rr* = Ξ΅ + r*r
(r + Ξ΅)* = r*
(r*s*)* = (r + s)*
r*r* = r*
NOTE THAT CONCATENATION IS **NOT** COMMUTATIVE: rs β sr in
general, since "ab" and "ba" are different strings. This is
the identity students most often assume by mistake.
ββ WORKED EXAMPLES OF DESCRIPTION ββββββββββββββββββββββββββ
Over Ξ£ = {a, b}:
(a + b)* all strings, including Ξ΅
(a + b)*abb all strings ENDING in "abb"
a*b* any a's followed by any b's
β NOTE this is NOT "equal numbers",
which is not regular at all
(a + b)*a(a + b)* strings containing at least one a
(aa + ab + ba + bb)* strings of EVEN LENGTH
b*(ab*ab*)* strings with an EVEN NUMBER of a's
(a + b)*aa(a + b)* containing "aa" as a substring
a(a + b)*b starting with a and ending with b
(b + ab)*(a + Ξ΅) strings with no two consecutive a's
THE TECHNIQUE FOR CONSTRUCTING ONE: describe the string as a
SEQUENCE OF PHASES, then write each phase. "Even number of
a's" becomes "some b's, then repeatedly (an a, some b's,
another a, some b's)" β which is exactly b*(ab*ab*)*.
Practical regular expressions, and what they cannot do
Four independently motivated definitions β an algebraic notation, two machine models and a grammar β all pick out exactly the same class of languages. That convergence is the strongest evidence the class is natural rather than an artefact of one definition, and it is the same style of argument that later supports the ChurchβTuring thesis.
π Go further: The regular expressions in most programming languages are not regular. Backreferences β where \1 matches whatever the first group captured β let you write (a*)b\1, which requires equal counts on both sides of the b and is therefore beyond any finite automaton. The cost of that extra power is exactly what the theory predicts: such patterns cannot be compiled to a DFA, so the engine must backtrack, and backtracking can take exponential time on adversarial input. Theoretical regularity and linear-time matching are the same property viewed from two directions. Search "backreferences not regular NP-complete regex matching".
π‘ Exam angle: give the inductive definition β three base cases, three operators β and be careful to distinguish β from Ξ΅. State the precedence (star, then concatenation, then union). Memorise the algebraic identities, especially r* = Ξ΅ + rr*, (r+Ξ΅)* = r* and (r*s*)* = (r+s)*, and remember that concatenation is not commutative. The commonest questions are to write a regular expression for a described language β build it as a sequence of phases β and to simplify or prove two expressions equal, for which mutual containment of the string sets always works. Know the four equivalent characterisations and the closure properties.
Syllabus points
Definition and operators
Regular expression identities
Create a free account to tick topics off, take notes as you read, watch the video lessons and get a day-by-day study plan built around your exam date.