Introduction

A regular expression (RE) is a concise notation for a language built from single symbols by union, concatenation and Kleene star. The languages denoted by regular expressions are exactly the regular languages, which are also exactly the languages recognized by finite automata (proved in Equivalence of Regular Expressions and Finite Automata).

In GATE, regular expressions appear as: simplify an expression, decide whether a string matches, convert between RE and DFA/NFA, or identify which expression denotes a given language. This note covers syntax, semantics, precedence, algebraic identities and worked matching examples. Conversion algorithms live in the equivalence note.

Prerequisites

Core concepts

Inductive definition

Fix an alphabet Σ\Sigma. The regular expressions over Σ\Sigma and the language L(r)L(r) of each expression rr are defined together.

Base cases.

  • ∅\emptyset is a regular expression, and L(∅)=∅L(\emptyset) = \emptyset.
  • ε\varepsilon is a regular expression, and L(ε)={ε}L(\varepsilon) = \{\varepsilon\}.
  • For every a∈Σa \in \Sigma, aa is a regular expression, and L(a)={a}L(a) = \{a\}.

Inductive cases. If rr and ss are regular expressions, then so are (r+s)(r+s), (rs)(rs) and (r∗)(r^*), with

L(r+s)=L(r)∪L(s),L(rs)=L(r)L(s),L(r∗)=(L(r))∗.L(r+s) = L(r) \cup L(s), \qquad L(rs) = L(r)L(s), \qquad L(r^*) = (L(r))^*.

Some texts write r∣sr \mid s or r∪sr \cup s for union; these notes use ++, matching Hopcroft-Motwani-Ullman.

Parentheses. Parentheses are part of the syntax. They may be dropped using the precedence rules below.

Precedence and associativity

Operator precedence, from highest to lowest:

  1. Kleene star ∗* (and usually +^+, if used as shorthand for rr∗rr^*).
  2. Concatenation (juxtaposition).
  3. Union ++.

Star is unary postfix and associates to the left in stacks of stars (a∗∗=(a∗)∗a^{**} = (a^*)^*). Concatenation and union are associative: L((rs)t)=L(r(st))L((rs)t) = L(r(st)) and L((r+s)+t)=L(r+(s+t))L((r+s)+t) = L(r+(s+t)). Union is commutative; concatenation is not.

Examples of parsing.

WrittenParsed asNot
a+b∗a+b^*a+(b∗)a+(b^*)(a+b)∗(a+b)^*
ab∗ab^*a(b∗)a(b^*)(ab)∗(ab)^*
a+bca+bca+(bc)a+(bc)(a+b)c(a+b)c
(a+b)∗(a+b)^*star of union

Misreading a+b∗a+b^* as (a+b)∗(a+b)^* is an error to avoid: the first language is {a}∪b∗\{a\} \cup b^*; the second is all strings over {a,b}\{a,b\}.

Shorthand

  • r+=rr∗r^+ = r r^* (one or more).
  • r?=r+εr^? = r + \varepsilon (optional), used in some tools; not required in the formal definition.
  • rkr^k means rr concatenated kk times (r0=εr^0 = \varepsilon).

Unix-style character classes and wildcards (., [0-9]) are Related material from lexical analysis tools; the formal GATE RE uses only ∅\emptyset, ε\varepsilon, symbols, ++, concatenation and ∗*.

How it works

Evaluating membership

To decide whether w∈L(r)w \in L(r), expand the expression by the inductive definition, or convert to an NFA and run it (see the equivalence note). For small expressions, case analysis works:

  • w∈L(r+s)w \in L(r+s) iff w∈L(r)w \in L(r) or w∈L(s)w \in L(s).
  • w∈L(rs)w \in L(rs) iff w=uvw = uv for some u∈L(r)u \in L(r), v∈L(s)v \in L(s).
  • w∈L(r∗)w \in L(r^*) iff w=u1…ukw = u_1 \ldots u_k for some k≥0k \ge 0 and each ui∈L(r)u_i \in L(r) (the case k=0k = 0 gives ε\varepsilon).

Algebraic identities

The following hold for all regular expressions r,s,tr,s,t (language equality):

IdentityForm
Associativityr+(s+t)=(r+s)+tr+(s+t) = (r+s)+t, r(st)=(rs)tr(st) = (rs)t
Commutativity of ++r+s=s+rr+s = s+r
Identitiesr+∅=rr+\emptyset = r, rε=εr=rr\varepsilon = \varepsilon r = r
Annihilatorr∅=∅r=∅r\emptyset = \emptyset r = \emptyset
Idempotencer+r=rr+r = r
Distributivityr(s+t)=rs+rtr(s+t) = rs+rt, (s+t)r=sr+tr(s+t)r = sr+tr
Star∅∗=ε\emptyset^* = \varepsilon, ε∗=ε\varepsilon^* = \varepsilon, (r∗)∗=r∗(r^*)^* = r^*, r∗=ε+rr∗=ε+r∗rr^* = \varepsilon + r r^* = \varepsilon + r^* r
More star(r+s)∗=(r∗s∗)∗(r+s)^* = (r^* s^*)^*, r(sr)∗=(rs)∗rr(sr)^* = (rs)^* r

These are proved by induction on expressions or by automata equivalence. The last row is useful when simplifying nested stars.

Test for an identity. A law E=FE = F whose variables r,s,…r, s, \ldots stand for arbitrary languages holds iff it holds after replacing each variable by a distinct fresh symbol and comparing the two resulting concrete expressions (Hopcroft-Motwani-Ullman, Section 3.4.7). So (r+s)∗=(r∗s∗)∗(r+s)^* = (r^* s^*)^* is a law because (a+b)∗=(a∗b∗)∗(a+b)^* = (a^* b^*)^* over {a,b}\{a,b\}, and (rs)∗=r∗s∗(rs)^* = r^* s^* is not, because abab is in L((ab)∗)L((ab)^*) but not in L(a∗b∗)L(a^* b^*).

Equivalence traps

ClaimTrue?Witness or reason
(a+b)∗=(a∗b∗)∗=(a∗b)∗a∗(a+b)^* = (a^* b^*)^* = (a^* b)^* a^*yesevery string is blocks of aas and bbs
a∗(ba∗)∗=(a+b)∗a^*(b a^*)^* = (a+b)^*yescut before each bb
(ab)∗a=a(ba)∗(ab)^* a = a(ba)^*yesshifting law r(sr)∗=(rs)∗rr(sr)^* = (rs)^* r
(aa)∗a=a(aa)∗(aa)^* a = a (aa)^*yesboth are odd numbers of aas
r∗r∗=r∗r^* r^* = r^*, (r∗)∗=r∗(r^*)^* = r^*, (ε+r)∗=r∗(\varepsilon + r)^* = r^*yesstar absorbs
ε+aa∗=a∗\varepsilon + a a^* = a^*yesr∗=ε+rr∗r^* = \varepsilon + r r^*
(a+b)∗=a∗+b∗(a+b)^* = a^* + b^*noab is only on the left
(ab)∗=a∗b∗(ab)^* = a^* b^*noaab only right, abab only left
(a∗b)∗=(a+b)∗(a^* b)^* = (a+b)^*noa only right: (a∗b)∗(a^* b)^* is ε\varepsilon or ends in bb
(a+b)∗a(a+b)∗b(a+b)∗=(a+b)∗ab(a+b)∗(a+b)^* a (a+b)^* b (a+b)^* = (a+b)^* ab (a+b)^*yesan aa before a later bb forces an adjacent ab
a∗∩b∗=∅a^* \cap b^* = \emptysetnoboth contain ε\varepsilon, so the intersection is {ε}\{\varepsilon\}

Writing expressions for standard languages

Over {a,b}\{a,b\}:

LanguageOne correct expression
even length((a+b)(a+b))∗((a+b)(a+b))^*
length divisible by 3((a+b)(a+b)(a+b))∗((a+b)(a+b)(a+b))^*
exactly two aasb∗ab∗ab∗b^* a b^* a b^*
at most one aab∗(ε+a)b∗b^* (\varepsilon + a) b^*
even number of aasb∗(ab∗ab∗)∗b^* (a b^* a b^*)^*
no two consecutive aas(b+ab)∗(ε+a)(b + ab)^* (\varepsilon + a)
does not contain abb∗a∗b^* a^*
first and last symbols differa(a+b)∗b+b(a+b)∗aa(a+b)^* b + b(a+b)^* a
ends with ab, alternative form(b∗a+b)+(b^* a^+ b)^+ where r+=rr∗r^+ = r r^*

Counting check. The number of strings of length nn with no two consecutive aas is the Fibonacci number Fn+2F_{n+2} (with F1=F2=1F_1 = F_2 = 1): 1, 2, 3, 5, 8, 13 for n=0,…,5n = 0, \ldots, 5. A quick count of short strings is a reliable way to catch a wrong expression.

Algorithms and pseudocode

Simplification is not unique; a practical greedy strategy:

Text
SIMPLIFY-RE(r)
1  rewrite nested stars: (r*)* -> r*
2  drop empty unions: r+emptyset -> r
3  drop empty concatenations: r emptyset -> emptyset
4  factor where obvious: rs+rt -> r(s+t)
5  apply r* = epsilon + r r* when it shortens a case split
6  return the rewritten expression

There is no canonical "simplest" RE in general (two minimal-state DFAs are unique up to renaming, but many REs can denote the same language). Distinct regular expressions can denote the same language; follow any syntax or form required by the question.

Mathematical explanation

Regular languages via expressions

A language is regular if some regular expression denotes it. Equivalent definitions (proved later): accepted by a DFA; accepted by an NFA; generated by a right-linear (Type-3) grammar.

Finite languages are regular: {w1,…,wm}=L(w1+⋯+wm)\{w_1,\ldots,w_m\} = L(w_1 + \cdots + w_m) (each wiw_i written as a concatenation of symbols). Σ∗\Sigma^* itself is L((a1+⋯+ak)∗)L((a_1+\cdots+a_k)^*) when Σ={a1,…,ak}\Sigma = \{a_1,\ldots,a_k\}.

Why star is the only iteration needed

Concatenation and union alone generate only finite languages. Star adds unbounded repetition, which is exactly what finite-memory machines can count in unary patterns such as a∗a^*, but not what they need for anbna^n b^n.

Worked examples

Example 1: membership in a∗b∗a^* b^*

L(a∗b∗)={aibj∣i,j≥0}L(a^* b^*) = \{ a^i b^j \mid i,j \ge 0 \}.

StringIn the language?Reason
ε\varepsilonyesi=j=0i=j=0
aaayesi=3,j=0i=3,j=0
bbyesi=0,j=2i=0,j=2
aaabbyesi=3,j=2i=3,j=2
banoa bb appears before an aa
abanoan aa after a bb

Example 2: a+b∗a + b^* versus (a+b)∗(a+b)^*

  • L(a+b∗)={a}∪{bk∣k≥0}={a,ε,b,bb,bbb,…}L(a+b^*) = \{a\} \cup \{ b^k \mid k \ge 0 \} = \{a, \varepsilon, b, bb, bbb, \ldots\}.
  • Strings not in it include ab, ba, aa.
  • L((a+b)∗)={a,b}∗L((a+b)^*) = \{a,b\}^*, every string.

So ab distinguishes them.

Example 3: simplify (a∗b∗)∗(b∗a∗)∗(a^* b^*)^* (b^* a^*)^*

Both (a∗b∗)∗(a^* b^*)^* and (b∗a∗)∗(b^* a^*)^* equal {a,b}∗\{a,b\}^* (any string can be split into blocks of aa's followed by bb's, or vice versa). Their concatenation is still {a,b}∗\{a,b\}^*. A simplified expression is (a+b)∗(a+b)^*.

Example 4: language of strings ending with 01

One expression: (0+1)∗01(0+1)^* 01.

Check: 01 matches (ε\varepsilon then 01); 101 matches; 0 does not; 010 does not (ends with 10).

A structurally different correct expression is (1∗00∗1)(1∗00∗1)∗(1^* 0 0^* 1)(1^* 0 0^* 1)^*: cut the string just after each occurrence of 01; every block is some 1s, then at least one 0, then a 1. Proving two expressions equal this way (a decomposition argument) is an alternative to building automata.

Example 5: ∅\emptyset and ε\varepsilon traps

  • L(a+∅)=L(a)={a}L(a + \emptyset) = L(a) = \{a\}.
  • L(a∅)=∅L(a \emptyset) = \emptyset.
  • L(∅∗)={ε}L(\emptyset^*) = \{\varepsilon\}.
  • L((∅+a)∗)=a∗L((\emptyset + a)^*) = a^*.

Important properties and observations

  • Precedence: ∗* before concatenation before ++.
  • Regular expressions denote regular languages; not every language has an RE (for example {anbn∣n≥0}\{a^n b^n \mid n \ge 0\}).
  • Many different REs denote the same language; minimization is for DFAs, not for REs.
  • L(r∗)L(r^*) always contains ε\varepsilon.
  • If L(r)=∅L(r) = \emptyset, then L(r∗)={ε}L(r^*) = \{\varepsilon\} and L(rs)=∅L(r s) = \emptyset.

Common misconceptions.

  • Reading a+b∗a+b^* as (a+b)∗(a+b)^*.
  • Thinking every infinite language needs an RE with star nested two deep; one star often suffices.
  • Claiming r+s=s+rr + s = s + r fails; union is commutative.
  • Claiming rs=srrs = sr always; concatenation is not commutative.

GATE examination perspective

  • PRECEDENCE TRAPS. Expand a+bc∗a+bc^* carefully before comparing languages.
  • WHICH STRINGS MATCH. Prefer converting a short RE to an NFA/DFA when membership of many strings is asked.
  • EQUIVALENCE OF TWO RES. Convert both to minimal DFAs and compare, or argue by identities. Do not rely on looking similar.
  • DESCRIBE THE LANGUAGE IN WORDS. Translate (0+1)∗1(0+1)∗(0+1)^* 1 (0+1)^* as "strings containing at least one 1".
  • LEXICAL REs. Token patterns in compilers are regular expressions; longest-match and priority rules are compiler issues, not pure language equality.
  • ONE COUNTEREXAMPLE KILLS AN IDENTITY. For a false claim, find the shortest string in one side and not the other; test ε\varepsilon, single symbols, then ab and ba.
  • ∅\emptyset VERSUS ε\varepsilon. ∅∗=ε\emptyset^* = \varepsilon but r∅=∅r \emptyset = \emptyset; ε\varepsilon is the identity for concatenation, ∅\emptyset for union.
  • "AT LEAST" AND "AT MOST". "At least one aa" is (a+b)∗a(a+b)∗(a+b)^* a (a+b)^*; "at most one" is b∗(ε+a)b∗b^*(\varepsilon + a)b^*. Check which one the wording asks for.

Summary

Sources

  1. J. E. Hopcroft, R. Motwani, J. D. Ullman, Introduction to Automata Theory, Languages, and Computation, 3rd ed., Pearson Addison-Wesley: Chapter 3, Sections 3.1 (regular expressions), 3.4 (algebraic laws).
  2. M. Sipser, Introduction to the Theory of Computation, 3rd ed., Cengage Learning: Chapter 1, Section 1.3 (regular expressions).
  3. P. Linz, An Introduction to Formal Languages and Automata, 6th ed., Jones & Bartlett Learning: Chapter 3, Section 3.1 (regular expressions).

Next steps