G27
Self-checkedTier 1 11 min readtoc.regular-expressions

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 Σ\Sigma be an alphabet. Regular expressions over Σ\Sigma and their languages L(r)L(r) are defined together by induction:

expression rrlanguage L(r)L(r)
∅\emptyset∅\emptyset, the empty language (no strings at all)
ε\varepsilon{ε}\{\varepsilon\}, only the empty string
aa for a∈Σa \in \Sigma{a}\{a\}
r∣sr \mid sL(r)∪L(s)L(r) \cup L(s) (union)
rsr sL(r)L(s)={xy:x∈L(r),y∈L(s)}L(r) L(s) = \{xy : x \in L(r), y \in L(s)\} (concatenation)
r∗r^*L(r)∗={x1x2⋯xk:k≥0, each xi∈L(r)}L(r)^* = \{x_1 x_2 \cdots x_k : k \ge 0, \text{ each } x_i \in L(r)\} (star)
(r)(r)L(r)L(r)

A language is regular iff it is L(r)L(r) for some regular expression rr (and, by the next page, iff some DFA or NFA accepts it).

Precedence (strongest first): star, then concatenation, then union. So a∣bc∗a \mid b c^* means a∣(b(c∗))a \mid (b (c^*)), and ab∗a b^* is a(b∗)a (b^*), not (ab)∗(ab)^*.

Derived forms. r+=rr∗r^+ = r r^* (one or more), r?=r∣εr? = r \mid \varepsilon (zero or one), and rk=rr⋯rr^k = r r \cdots r (kk copies).

Notation warning. This site writes union as ∣\mid. Sipser writes ∪\cup and many GATE papers write a plus sign: in a GATE paper a+ba + b usually means "aa or bb", and a+a^+ is a superscript plus meaning "one or more". Read the question's convention before anything else.

Nullable. ε∈L(r)\varepsilon \in L(r) exactly when rr is nullable: ε\varepsilon and r∗r^* are nullable; a symbol and ∅\emptyset are not; r∣sr \mid s is nullable if either is; rsr s is nullable if both are.

How it works

Reading an expression

Take the language of rr piece by piece.

  • (a∣b)∗(a \mid b)^*: every string over {a,b}\{a, b\}, including ε\varepsilon.
  • a∗b∗a^* b^*: some aa's followed by some bb's; equivalently the strings that do not contain baba.
  • (ab)∗(ab)^*: ε\varepsilon, abab, abababab, …\ldots
  • (a∣b)∗abb(a \mid b)^* a b b: strings that end in abbabb.
  • a(a∣b)∗a∣aa (a \mid b)^* a \mid a: nonempty strings that start with aa and end with aa (the single string aa is added by the last alternative).

The special cases

∅\emptyset and ε\varepsilon are different: L(∅)L(\emptyset) has no string, L(ε)L(\varepsilon) has one, the empty string. Therefore

r∅=∅r=∅,rε=εr=r,r∣∅=r,∅∗=ε∗=ε.r \emptyset = \emptyset r = \emptyset, \qquad r \varepsilon = \varepsilon r = r, \qquad r \mid \emptyset = r, \qquad \emptyset^* = \varepsilon^* = \varepsilon.

Algebraic laws: which hold

Union is commutative, associative and idempotent (r∣r=rr \mid r = r). Concatenation is associative but not commutative. Concatenation distributes over union on both sides. For star: (r∗)∗=r∗(r^*)^* = r^*, r∗r∗=r∗r^* r^* = r^*, rr∗=r∗rr r^* = r^* r, (r∣ε)∗=r∗(r \mid \varepsilon)^* = r^*, (r∣s)∗=(r∗s∗)∗=(r∗s)∗r∗(r \mid s)^* = (r^* s^*)^* = (r^* s)^* r^*, and (rs)∗r=r(sr)∗(r s)^* r = r (s r)^* (the "sliding rule").

The table tests fifteen of the claims, each on a concrete choice (for instance r=ar = a, s=bs = b) by comparing the two languages on all strings up to length 77. 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
identitytested withagree?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.

0 / 15
computed by script: the machine is simulated or the sets are constructed by scripts/toc_lib.py, one step at a time. python3 scripts/gen-toc2-traces.py

The popular false ones: (r∣s)∗≠r∗∣s∗(r \mid s)^* \ne r^* \mid s^* (the left side contains abab, the right does not); (rs)∗≠r∗s∗(rs)^* \ne r^* s^* (the left side is ε,ab,abab,…\varepsilon, ab, abab, \ldots, the right side contains aa); rs≠srrs \ne sr (concatenation does not commute); and union does not distribute over concatenation: r∣st≠(r∣s)(r∣t)r \mid s t \ne (r \mid s)(r \mid t) 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 1010.

languageexpression
binary strings ending in 0101(0∣1)∗01(0 \mid 1)^* 01
binary strings containing 0000(0∣1)∗00(0∣1)∗(0 \mid 1)^* 00 (0 \mid 1)^*
binary strings with no 1111(0∣10)∗(1∣ε)(0 \mid 10)^* (1 \mid \varepsilon)
an even number of 11s0∗(10∗10∗)∗0^* (1 0^* 1 0^*)^*
length divisible by 33 over {a,b}\{a, b\}((a∣b)(a∣b)(a∣b))∗((a \mid b)(a \mid b)(a \mid b))^*
binary strings (nonempty) with the same first and last symbol0(0∣1)∗0∣1(0∣1)∗1∣0∣10 (0 \mid 1)^* 0 \mid 1 (0 \mid 1)^* 1 \mid 0 \mid 1
the third symbol from the end is 11(0∣1)∗1(0∣1)(0∣1)(0 \mid 1)^* 1 (0 \mid 1)(0 \mid 1)
no baba (all aa's first)a∗b∗a^* b^*
exactly two aa's over {a,b}\{a, b\}b∗ab∗ab∗b^* a b^* a b^*
at most one aab∗∣b∗ab∗b^* \mid b^* a b^*
ends with abbabb(a∣b)∗abb(a \mid b)^* abb
number of aa's divisible by 33b∗(ab∗ab∗ab∗)∗b^* (a b^* a b^* a b^*)^*
every aa is immediately followed by bb(b∣ab)∗(b \mid ab)^*
even binary numbers (with ε\varepsilon)(0∣1)∗0∣ε(0 \mid 1)^* 0 \mid \varepsilon
strings made of pairs aaaa and bbbb(aa∣bb)∗(aa \mid bb)^*

Two techniques are behind most of these: a gap pattern X∗ mark X∗X^* \, \text{mark} \, X^* for "contains" and "at least", and a loop-and-exit pattern for "exactly kk" or "divisible by kk" (a block that contains one counted symbol, repeated).

Counting strings

The number of strings of length nn in L(r)L(r) can be read off from the expression when it is unambiguous. (a∣b)∗a(a \mid b)^* a has 2n−12^{n-1} strings of length nn. The strings that contain abab number 2n−(n+1)2^n - (n + 1) (the ones that do not contain abab are biajb^i a^j, which are n+1n + 1 of them). The language (b∣ab)∗(b \mid ab)^* has 1,1,2,3,5,8,13,211, 1, 2, 3, 5, 8, 13, 21 strings of lengths 0,1,2,…,70, 1, 2, \ldots, 7: each length is built from blocks bb (length 11) and abab (length 22), 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 {anbn}\{a^n b^n\} 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

L(a∗b)L(a^* b): strings of aa's followed by one bb (b,ab,aab,…b, ab, aab, \ldots). L(a(b∣c)∗)L(a (b \mid c)^*): aa followed by any string of bb's and cc's. L(∅∗)={ε}L(\emptyset^*) = \{\varepsilon\}. L(a∅)=∅L(a \emptyset) = \emptyset. L((ε∣a)(ε∣b))={ε,a,b,ab}L((\varepsilon \mid a)(\varepsilon \mid b))= \{\varepsilon, a, b, ab\}.

EX 2Write the expression

Easy

Binary strings that start with 11 and end with 00: 1(0∣1)∗01 (0 \mid 1)^* 0 (note that "1010" is the shortest; the single symbols are not in the language). Binary strings of length exactly 44: (0∣1)(0∣1)(0∣1)(0∣1)(0 \mid 1)(0 \mid 1)(0 \mid 1)(0 \mid 1). Strings over {a,b}\{a, b\} with an even number of aa's: b∗(ab∗ab∗)∗b^* (a b^* a b^*)^*.

EX 3Are these equal

Medium

Is (a∣b)∗=(a∗b∗)∗(a \mid b)^* = (a^* b^*)^*? Yes: the right side can produce any sequence of aa's and bb's by choosing blocks of one letter. Is (a∣b)∗=a∗∣b∗(a \mid b)^* = a^* \mid b^*? No: abab is on the left only. Is a∗(ba∗)∗=(a∣b)∗a^* (b a^*)^* = (a \mid b)^*? Yes (any string is aa's followed by blocks that start with bb). Is (ab)∗a=a(ba)∗(ab)^* a = a (ba)^*? Yes (sliding rule). Is (a∣b)∗ab(a∣b)∗=(a \mid b)^* a b (a \mid b)^* = the strings containing abab, and is b∗a∗b^* a^* its complement? Yes: a string has no abab iff all its bb's come before its aa's.

EX 4Simplify

Medium

∅∗∣a∗\emptyset^* \mid a^*: ∅∗=ε\emptyset^* = \varepsilon and ε∈L(a∗)\varepsilon \in L(a^*), so the language is L(a∗)L(a^*). (a∗b∗)∗(a∣b)∗(a^* b^*)^* (a \mid b)^*: both parts equal (a∣b)∗(a \mid b)^* and (a∣b)∗(a∣b)∗=(a∣b)∗(a \mid b)^*(a \mid b)^* = (a \mid b)^*. a(ε∣a)∗a (\varepsilon \mid a)^* : (ε∣a)∗=a∗(\varepsilon \mid a)^* = a^* so the language is aa∗=a+a a^* = a^+.

EX 5Typical questions

GATE level

(a) Which expression describes strings with no two consecutive 11s? (0∣10)∗(1∣ε)(0 \mid 10)^* (1 \mid \varepsilon); the shorter-looking (0∗1)∗(0^* 1)^* is wrong because it forces the string to end in 11 or be empty. (b) Which strings of length 22 are in L((a∣b)∗a)L((a \mid b)^* a)? aaaa and baba: 22−1=22^{2-1} = 2. (c) Number of strings of length 44 in L((a∣b)∗ab(a∣b)∗)L((a \mid b)^* ab (a \mid b)^*): 24−5=112^4 - 5 = 11. (d) Is (a∗b)∗=(a∣b)∗b∣ε(a^* b)^* = (a \mid b)^* b \mid \varepsilon? Yes: both are ε\varepsilon and the strings that end in bb. (e) Minimum number of states of a DFA for (0∣1)∗0(0∣1)(0 \mid 1)^* 0 (0 \mid 1) (second symbol from the end is 00): 44 (it remembers the last two symbols); the NFA needs 33.

Cheat box

Cheat box

  • ∅\emptyset: no string; ε\varepsilon: just the empty string; r∅=∅r\emptyset = \emptyset, rε=rr\varepsilon = r, ∅∗=ε\emptyset^* = \varepsilon.
  • Precedence: star, concatenation, union. ab∗=a(b∗)ab^* = a(b^*).
  • True: (r∗)∗=r∗(r^*)^* = r^*, (r∣s)∗=(r∗s∗)∗=(r∗s)∗r∗(r \mid s)^* = (r^* s^*)^* = (r^* s)^* r^*, rr∗=r∗rr r^* = r^* r, (rs)∗r=r(sr)∗(rs)^* r = r (sr)^*, distribution of concatenation over union.
  • False: (r∣s)∗=r∗∣s∗(r \mid s)^* = r^* \mid s^*, (rs)∗=r∗s∗(rs)^* = r^* s^*, rs=srrs = sr.
  • GATE often writes union as ++; r+r^+ means one or more.
  • Regular expressions, DFAs, NFAs: the same class of languages.

Traps and GATE patterns

Connections

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

GATE 2024 · Set 1 · NAT · 2 marksQ14 · page 199 of the book
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 _________
GATE 2024 · Set 2 · NAT · 2 marksQ17 · page 199 of the book
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 ___________
GATE 2020 · MCQQ23 · page 200 of the book
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
GATE 2016 · Set 1 · MCQQ36 · page 202 of the book
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
GATE 2015 · Set 2 · MCQ · 2 marksQ41 · page 202 of the book
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
GATE 2014 · Set 3 · MCQ · 1 markQ43 · page 202 of the book
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
GATE 2013 · Set 1 · MCQ · 2 marksQ49 · page 203 of the book
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
GATE 2010 · MCQ · 2 marksQ58 · page 205 of the book
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 L(∅∗)L(\emptyset^*) and what is L(a∅)L(a \emptyset)?

Show answer

L(∅∗)={ε}L(\emptyset^*) = \{\varepsilon\} (zero copies); L(a∅)=∅L(a \emptyset) = \emptyset.

Question 2

Is (a∣b)∗=a∗∣b∗(a \mid b)^* = a^* \mid b^*?

Show answer

No. The string abab is in the left language only.

Question 3

Write an expression for the strings over {a,b}\{a, b\} in which every aa is immediately followed by bb.

Show answer

(b∣ab)∗(b \mid ab)^*. It excludes strings ending in aa and strings containing aaaa; it has 1,1,2,3,5,8,…1, 1, 2, 3, 5, 8, \ldots strings of lengths 0,1,2,…0, 1, 2, \ldots.

Question 4

Does ab∗a b^* equal (ab)∗(ab)^*?

Show answer

No. ab∗a b^* is aa followed by bb's (a,ab,abb,…a, ab, abb, \ldots); (ab)∗(ab)^* contains ε\varepsilon and abababab.

Question 5

How many strings of length 33 does (a∣b)∗a(a \mid b)^* a have?

Show answer

44: every string of length 33 ending in aa, which is 222^2.

Revision card

One-screen revision

  • Base: ∅\emptyset, ε\varepsilon, symbols; operators union, concatenation, star; precedence star over concatenation over union.
  • Same class as DFAs and NFAs.
  • True: (r∣s)∗=(r∗s∗)∗(r \mid s)^* = (r^* s^*)^*, (rs)∗r=r(sr)∗(rs)^* r = r(sr)^*, (r∗)∗=r∗(r^*)^* = r^*. False: (r∣s)∗=r∗∣s∗(r \mid s)^* = r^* \mid s^*, (rs)∗=r∗s∗(rs)^* = r^* s^*.
  • r∅=∅r\emptyset = \emptyset, rε=rr\varepsilon = r, ∅∗=ε\emptyset^* = \varepsilon. 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