Regular expressions
Regular expressions as a notation for languages: the inductive definition (empty set, epsilon, symbols, union, concatenation, star), precedence, the language of an expression, derived operators, the algebraic laws that hold and the popular ones that fail, writing expressions for the standard GATE languages, counting strings, and what regular expressions cannot say.
Why it exists
A DFA or NFA describes a regular language by a machine; a regular expression describes it by a formula, which is the form people and programs prefer for searching text and for specifying the tokens of a language. GATE asks both directions: write an expression for a described language, and decide what language an expression describes or which of several expressions are equivalent.
Prerequisites
You need NFAs and languages as sets of strings (NFA); the equivalence of the two notations is the next page (regex and automata).
Precise definitions
Let be an alphabet. Regular expressions over and their languages are defined together by induction:
| expression | language |
|---|---|
| , the empty language (no strings at all) | |
| , only the empty string | |
| for | |
| (union) | |
| (concatenation) | |
| (star) | |
A language is regular iff it is for some regular expression (and, by the next page, iff some DFA or NFA accepts it).
Precedence (strongest first): star, then concatenation, then union. So means , and is , not .
Derived forms. (one or more), (zero or one), and ( copies).
Notation warning. This site writes union as . Sipser writes and many GATE papers write a plus sign: in a GATE paper usually means " or ", and is a superscript plus meaning "one or more". Read the question's convention before anything else.
Nullable. exactly when is nullable: and are nullable; a symbol and are not; is nullable if either is; is nullable if both are.
How it works
Reading an expression
Take the language of piece by piece.
- : every string over , including .
- : some 's followed by some 's; equivalently the strings that do not contain .
- : , , ,
- : strings that end in .
- : nonempty strings that start with and end with (the single string is added by the last alternative).
The special cases
and are different: has no string, has one, the empty string. Therefore
Algebraic laws: which hold
Union is commutative, associative and idempotent (). Concatenation is associative but not commutative. Concatenation distributes over union on both sides. For star: , , , , , and (the "sliding rule").
The table tests fifteen of the claims, each on a concrete choice (for instance , ) by comparing the two languages on all strings up to length . A row marked false has a witness string and is false in general; the standard true laws agree on all tested strings, and each is also a known theorem.
Which regular expression identities are true?: start
Computed by a script, not typed| identity | tested with | agree? | witness |
|---|---|---|---|
| (r*)* = r* | |||
| r* r* = r* | |||
| r r* = r* r | |||
| (r | s)* = (r* s*)* | |||
| (r | s)* = r* | s* | |||
| (r s)* = r* s* | |||
| r (s | t) = r s | r t | |||
| (r | s) t = r t | s t | |||
| r s = s r | |||
| ε r = r | |||
| ∅ r = ∅ | |||
| (r | ε)* = r* | |||
| r* | ε = r* | |||
| (r s)* r = r (s r)* | |||
| (r* s)* r* = (r | s)* |
Step 0
Each identity is tested on a concrete choice of r, s, t by comparing the two languages on all strings up to length 7. A single difference proves the identity false; agreement on the sample is, for the standard laws, backed by proof.
The popular false ones: (the left side contains , the right does not); (the left side is , the right side contains ); (concatenation does not commute); and union does not distribute over concatenation: in general.
Writing expressions for the standard languages
Each expression below was compared with an independent predicate on every string of the stated alphabet up to length .
| language | expression |
|---|---|
| binary strings ending in | |
| binary strings containing | |
| binary strings with no | |
| an even number of s | |
| length divisible by over | |
| binary strings (nonempty) with the same first and last symbol | |
| the third symbol from the end is | |
| no (all 's first) | |
| exactly two 's over | |
| at most one | |
| ends with | |
| number of 's divisible by | |
| every is immediately followed by | |
| even binary numbers (with ) | |
| strings made of pairs and |
Two techniques are behind most of these: a gap pattern for "contains" and "at least", and a loop-and-exit pattern for "exactly " or "divisible by " (a block that contains one counted symbol, repeated).
Counting strings
The number of strings of length in can be read off from the expression when it is unambiguous. has strings of length . The strings that contain number (the ones that do not contain are , which are of them). The language has strings of lengths : each length is built from blocks (length ) and (length ), and the counts follow the Fibonacci numbers. These counts were recomputed by brute force.
What regular expressions cannot say
A regular expression has a finite description with no counter, so languages such as or the balanced parentheses have no regular expression (proved by the pumping lemma, pumping lemma). Programming-language "regex" engines add features such as backreferences that go beyond regular expressions (quoted, not verified here); this page speaks only about the theoretical operators.
Worked examples
EX 1Which strings
Easy: strings of 's followed by one (). : followed by any string of 's and 's. . . .
EX 2Write the expression
EasyBinary strings that start with and end with : (note that "" is the shortest; the single symbols are not in the language). Binary strings of length exactly : . Strings over with an even number of 's: .
EX 3Are these equal
MediumIs ? Yes: the right side can produce any sequence of 's and 's by choosing blocks of one letter. Is ? No: is on the left only. Is ? Yes (any string is 's followed by blocks that start with ). Is ? Yes (sliding rule). Is the strings containing , and is its complement? Yes: a string has no iff all its 's come before its 's.
EX 4Simplify
Medium: and , so the language is . : both parts equal and . : so the language is .
EX 5Typical questions
GATE level(a) Which expression describes strings with no two consecutive s? ; the shorter-looking is wrong because it forces the string to end in or be empty. (b) Which strings of length are in ? and : . (c) Number of strings of length in : . (d) Is ? Yes: both are and the strings that end in . (e) Minimum number of states of a DFA for (second symbol from the end is ): (it remembers the last two symbols); the NFA needs .
Cheat box
Cheat box
- : no string; : just the empty string; , , .
- Precedence: star, concatenation, union. .
- True: , , , , distribution of concatenation over union.
- False: , , .
- GATE often writes union as ; means one or more.
- Regular expressions, DFAs, NFAs: the same class of languages.
Traps and GATE patterns
Connections
Builds on
Across subjects
Every regular expression has an equivalent NFA of linear size and every DFA an equivalent expression (regex and automata); scanner generators take token patterns written as regular expressions (lexical analysis); the design techniques for DFAs and expressions mirror each other (DFA design patterns).
PYQs
8 previous year questions from your book. I re-solved each one myself (8 by running or simulating, 0 by reasoning only); the book's key is shown next to my answer. All PYQs
Consider the following two regular expressions over the
alphabet {0,1}:
r = 0* + 1*
s = 01* + 10*
The total number of strings of length less than or equal to
5, which are neither in r nor in s, is _________Let L1 be the language represented by the regular expression
b *ab * (ab *ab * ) * and L2 = { w ∈ (a + b) * | |w| ≤ 4}, where
|w| denotes the length of string w. The number of strings in
L2 which are also in L1 is ___________Which one of the following regular expressions represents the set of all binary strings with an odd number of 1's?
- (a)10*(0*10*10*)*
- (b)((0 + 1)*1(0 + 1)*1)*10*
- (c)(0*10*10*)*10*
- (d)(0*10*10*)*0*1
Which one of the following regular expressions represents the language: the set of all binary strings having two consecutive 0s and two consecutive 1s?
- (a). (0 + 1)* 0011(0 + 1)* + (0 + 1)* 1100(0 + 1)*
- (b). (0 + 1)* (00(0 + 1)* 11 + 11(0 + 1)* 00)(0 + 1)*
- (c)(0 + 1)* 00(0 + 1)* + (0 + 1)* 11(0 + 1)*
- (d). 00(0 + 1)* 11 + 11(0 + 1)* 00
The number of states in the minimal deterministic finite automaton corresponding to the regular expression (0 + 1) * (10) is __________
- (a)3
- (b)4
- (c)5
- (d)6
The length of the shortest string NOT in the language (over
Σ = {a,b}) of the following regular expression is
a*b*(ba)*a* is ______________.- (a)3
- (b)4
- (c)5
- (d)6
Consider the DFA given.
Which of the following are FALSE?
1. Complement of L(A) is context-free.
2. L(A) = L((11*0+0)(0 + 1)*0*1*)
3. For the language accepted by A, A is the minimal
DFA.
4. A accepts all strings over {0, 1} of length at least 2.- (a)1 and 3 only
- (b)2 and 4 only
- (c)2 and 3 only
- (d)3 and 4 only
Let L = {w ∈ (0 + 1)* | w has even number of 1s}, i.e. L
is the set of all bit strings with even number of 1s. Which
one of the regular expression below represents L?- (a)(0 * 10 * 1)*
- (b)0* (10 * 10*)*
- (c)0*(10 * 1*)*0*
- (d)0 *1(10 * 1)*10*
Self-check
Question 1
What is and what is ?
Show answerHide
(zero copies); .
Question 2
Is ?
Show answerHide
No. The string is in the left language only.
Question 3
Write an expression for the strings over in which every is immediately followed by .
Show answerHide
. It excludes strings ending in and strings containing ; it has strings of lengths .
Question 4
Does equal ?
Show answerHide
No. is followed by 's (); contains and .
Question 5
How many strings of length does have?
Show answerHide
: every string of length ending in , which is .
Revision card
One-screen revision
- Base: , , symbols; operators union, concatenation, star; precedence star over concatenation over union.
- Same class as DFAs and NFAs.
- True: , , . False: , .
- , , . A plus sign is union in many GATE papers.
References
- ·Sipser, Introduction to the Theory of Computation (3rd edition), Section 1.3 (regular expressions)
- ·Hopcroft, Motwani, Ullman, Introduction to Automata Theory, Languages, and Computation (3rd edition), Chapter 3 (regular expressions and languages), Section 3.4 (algebraic laws)
- ·Aho, Lam, Sethi, Ullman, Compilers: Principles, Techniques, and Tools (2nd edition), Section 3.3 (specification of tokens)
Verification
scripts/enum-checks/toc_regular.py: every expression quoted on this page (17 of them) equals an independent Python predicate AND Python's own re module on every string over its alphabet up to length 10; derivatives, Thompson NFAs and DFAs agree with re on 300 random expressions; the true algebraic laws were tested on random triples of expressions on all strings up to length 6 and every 'false law' has a witness string; ε in L(r) agrees with the structural nullable test; the counts of strings of length n (powers of two, 2^n - (n + 1), Fibonacci numbers) were recomputed by brute force. The remark about backreferences in programming-language regexes is quoted, not verified here. The union operator is written with a vertical bar on this site; GATE papers often write a plus sign (see the trap). Textbook chapter references are from memory. SECOND PASS (2026-11-02, done by the assistant, not a human check): every Self-check question was solved before the printed answer was read and all agreed; the definitions, worked examples, cheat box and traps were read claim by claim and the numbers re-derived by hand or by a small script (state counts, derivations, parse-tree and move counts, decomposition counts); tables and animations are machine output and were read, not re-run; No other defect found. Status raised to self-checked; verified needs a textbook or human check.
last verified: never
My notes (saved in this browser)