Regular expressions and the pumping lemma, with the exam angle

Regular expressions and the pumping lemma explained: the operators, closure properties, and a worked proof that a^n b^n is not regular, with the exam angle.

Prashant Jain

KnowledgeGate AI educator

11 Jul 20265 min read

A regular expression is a compact algebraic way to describe a language, and the pumping lemma is the tool that proves a language is too complex to be described that way. Together they draw the exact boundary of what finite-memory machines can recognise. The pumping lemma in particular scares students, but it is a single fixed template, and once you have run it once you can run it on anything.

Let us build both up carefully.

Regular expressions: the three operators

A regular expression over an alphabet Σ is built from the symbols of Σ, the empty string ε, and the empty language, using just three operators:

  • Union, written + or |, meaning "either". The expression a + b denotes the language {a, b}.

  • Concatenation, written by juxtaposition, meaning "followed by". ab denotes {ab}.

  • Kleene star, written *, meaning "zero or more repetitions". a* denotes {ε, a, aa, aaa, ...}.

Precedence runs star, then concatenation, then union, and parentheses override it. A few readings to fix the idea: (a + b)* is every string over {a, b}, including the empty one; a*b* is any number of a's followed by any number of b's; (0 + 1)*01 is every binary string that ends in 01. The language denoted by a regular expression is simply the set of all strings it can generate.

The equivalence at the heart of the topic

Regular expressions are not a separate world from the automata of the previous topic. A foundational theorem, Kleene's theorem, says that regular expressions, DFAs, and NFAs all describe exactly the same class: the regular languages. A language is regular if and only if some regular expression denotes it, which holds if and only if some finite automaton, DFA or NFA, accepts it. Three notations, one class of languages.

Closure properties

The regular languages are closed under a generous list of operations: if L1 and L2 are regular, then so are their union, concatenation, Kleene star, intersection, complement, and difference. Closed means the result is guaranteed to still be regular. This matters for the exam because a common trick is to prove a language non-regular indirectly: if intersecting your language with a known regular language yields something you already know is non-regular, then your language could not have been regular either, since intersection preserves regularity.

The pumping lemma for regular languages

Every regular language has a property that non-regular languages lack, and that property is the pumping lemma.

Statement. If L is regular, then there exists a constant p (the pumping length) such that every string w in L with |w| ≥ p can be split into three parts, w = xyz, satisfying all three conditions:

  1. |y| ≥ 1, so the middle part is non-empty.

  2. |xy| ≤ p, so the split happens within the first p symbols.

  3. For every i ≥ 0, the pumped string xy^i z is also in L.

The intuition: a DFA with p states, reading a string of length p or more, must revisit some state, and that repeated visit is a loop. The substring read around the loop is y, and you can traverse the loop any number of times (including zero) and still be accepted. That loop is exactly what a finite machine cannot avoid, and it is what we exploit.

A worked proof: a^n b^n is not regular

Claim. The language L = { a^n b^n : n ≥ 0 }, equal numbers of a's then b's, is not regular.

We argue by contradiction using the lemma.

Step 1. Assume L is regular. Then the pumping lemma gives a pumping length p.

Step 2. Choose a specific string. Let w = a^p b^p. It is in L, and its length is 2p, which is at least p, so the lemma applies to it.

Step 3. By the lemma, w = xyz with |xy| ≤ p and |y| ≥ 1. Because the first p symbols of w are all a's, and xy sits within those first p symbols, both x and y consist only of a's. So y = a^k for some k ≥ 1.

Step 4. Pump. Take i = 2, giving xy^2z = a^(p+k) b^p. Since k ≥ 1, this string has more a's than b's, so it is not of the form a^n b^n and therefore is not in L.

Step 5. But condition 3 insists xy^2z must be in L. That is a contradiction. Our only assumption was that L is regular, so L is not regular.

The template never changes: assume regular, pick a clever w that depends on p, show every legal split can be pumped out of the language, conclude non-regularity. The whole skill is choosing w so that the |xy| ≤ p constraint traps y inside a region you can exploit.

Pumping-lemma questions in GATE, UGC NET and placements

GATE CS tests both halves. On regular expressions it asks which expression denotes a given language, or whether two expressions are equivalent. On the pumping lemma it asks you to identify which languages are regular, since classics like a^n b^n, ww, and { a^n b^n c^n } are non-regular and appear repeatedly. Closure properties are a reliable one-mark source. Our GATE CS exam category sequences this with automata and grammars.

UGC NET Computer Science favours the statement of the lemma, the closure-property list, and matching expressions to languages. Reproduce the three conditions exactly.

Placement and company tests rarely demand the formal proof, but the underlying idea, that regular expressions cannot count unbounded matched pairs, comes up whenever interviewers discuss why you should not parse nested structures with a plain regex.

KnowledgeGate's published question bank carries over one thousand Theory of Computation questions, and regular languages is one of its densest sub-areas, so there is ample drill on exactly these patterns.

Practise it, do not just read it

The pumping lemma only stops feeling mysterious after you have run the template on two or three languages yourself.

Learn the three operators, memorise the three pumping conditions, and hand-write the a^n b^n proof until the structure is second nature. Do that, and the pumping lemma turns from a fear into marks you can count on.