Track progress, take quizzes and save notes on this lesson.

Free forever · no card needed

Start free
Advanced

Regular Expressions

4.4.2.3 Regular expressions·4.4.2.4 Regular language

Aligned to the AQA 7517 specification

Level
Advanced
Reading time
8 min
Published
6 June 2026
Updated
1 July 2026
On this page
  1. 1.What Is a Regular Expression?
  2. 2.The Five Metacharacters
  3. 3.Reading Regular Expressions
  4. 4.Writing Regular Expressions
  5. 5.Regular Languages
  6. 6.Regular Expressions and Finite State Machines
  7. 7.Common Exam Mistakes

Key takeaways

  • A regular expression is a compact notation describing a set of strings (a language); every string that matches the pattern is a member of that set.
  • The five metacharacters assessed by AQA are `*`, `+`, `?`, `|` and `( )`; any character that is not a metacharacter is a literal that matches itself.
  • `a*` includes the empty string (zero or more repetitions), whereas `a+` requires at least one repetition and excludes the empty string.
  • A language is regular if and only if it can be represented by a regular expression, which is equivalent to being accepted by a finite state machine.
  • Regular expressions can only describe regular languages, so patterns needing counting like {0ⁿ1ⁿ | n ≥ 1} cannot be expressed and require formalisms such as BNF.

What Is a Regular Expression?

A regular expression is a compact notation for describing a set of strings — a language. Rather than listing every string in the set individually, a regular expression defines a pattern, and any string that matches the pattern is a member of the described set.

Example from the AQA spec: the regular expression a(a|b)* generates the set {a, aa, ab, aaa, aab, aba, …} — every string that starts with 'a' and is followed by any sequence of a's and b's (including no further characters).

Regular expressions are used throughout computing:

  • Compilers use them in lexical analysis to recognise tokens such as identifiers, keywords, and literals
  • Text editors and command-line tools use them for pattern matching and search-replace operations
  • Input validation (checking that a postcode or email address matches the expected format) is often implemented using regular expressions

A regular expression is a way of describing a set. Every string in that set matches the regular expression. Strings outside the set do not match.

The five metacharacters assessed by AQA are: *, +, ?, |, and ( ). Any other metacharacter used in an exam question will be defined within that question.

The Five Metacharacters

These five metacharacters are the building blocks of AQA regular expressions. Any character that is not a metacharacter is a literal — it matches itself exactly.

MetacharacterNameMeaningExample
*Star / Kleene star0 or more repetitions of the preceding elementab* matches a, ab, abb, abbb, …
+Plus1 or more repetitions of the preceding elementab+ matches ab, abb, abbb, … (not a alone)
?Question mark0 or 1 repetitions — makes the preceding element optionalcolou?r matches color or colour
|Pipe / alternationEither the left or the right — logical ORcat|dog matches cat or dog
( )ParenthesesGrouping — applies an operator to a sub-expression(ab)* matches ε, ab, abab, ababab, …

The difference between * and +:

  • a* includes the empty string (zero repetitions)
  • a+ requires at least one a — the empty string does not match

How ? works:

  • u? means "zero or one u" — the element is optional
  • colou?r = colo(u?)r = "colo, then optionally u, then r"

Grouping with parentheses:

  • Without grouping: ab* means "a, then zero or more b's" — the * applies only to b
  • With grouping: (ab)* means "zero or more copies of ab" — the * applies to the whole group ab

Reading Regular Expressions

The key skill is parsing a regular expression left to right, respecting operator precedence: *, +, ? bind tightly to the single preceding element (or group); | has lower precedence and separates alternatives.

Worked examples:

a(a|b)*

  • a → a literal 'a' (required)
  • (a|b) → either an 'a' or a 'b'
  • (a|b)* → zero or more repetitions of (a|b)
  • Full match: one 'a', followed by any sequence (including empty) of a's and b's
  • Matches: a, aa, ab, aaa, aab, aba, abba, …
  • Does NOT match: b, ba, empty string

(0|1)*1

  • (0|1)* → any sequence of 0s and 1s (including empty)
  • Followed by a literal 1
  • Full match: any binary string that ends with 1
  • Matches: 1, 01, 11, 001, 101, 111, …
  • Does NOT match: 0, 10, 100, empty string

ab?c

  • a → literal a
  • b? → optional b (0 or 1 times)
  • c → literal c
  • Matches: ac, abc
  • Does NOT match: abbc, a, bc

Writing Regular Expressions

Building a regular expression from a description requires identifying what is fixed, what repeats, and what is optional.

Strategy:

  1. Identify the required literals and their positions
  2. Identify what can repeat — use * (0 or more) or + (1 or more)
  3. Identify alternatives — use | with grouping ( )
  4. Identify optional parts — use ?

Worked example — write a RE matching non-empty binary strings with an even number of digits:

An even-length binary string can be seen as pairs of digits: (0|1)(0|1) repeated one or more times.

RE: ((0|1)(0|1))+

  • (0|1) → any single binary digit
  • (0|1)(0|1) → any pair of binary digits
  • ((0|1)(0|1))+ → one or more pairs

Matches: 00, 01, 10, 11, 0000, 0101, 1100, … Does not match: 0, 1, 000, 010 (odd-length strings)

Note on + vs * here: the empty string has length 0, which is technically even. Using + (one or more pairs) deliberately excludes it. If the empty string should be included, replace + with *: ((0|1)(0|1))* — this matches all binary strings with an even number of digits, including the empty string. This distinction (whether * includes the empty string and + does not) is assessed by AQA.

Worked example — write a RE matching strings over {a, b} that begin and end with 'a':

The string must start with 'a' and end with 'a'. The middle (which could be empty) can be anything over {a, b}.

RE: a(a|b)*a

This works for strings of length ≥ 2. But "a" alone (length 1) also starts and ends with 'a'. Combine:

RE: a(a|b)*a|a → or equivalently a((a|b)*a)?

The second form: 'a' followed by an optional group ((a|b)*a) — the optional part handles the middle content and the final 'a'.

Want more lessons like this one?

Generate lessons on anything you study. Free account, no card needed.

Start generating

Regular Languages

A language is any set of strings over a given alphabet. A language is called regular if it can be described by a regular expression — equivalently, if a finite state machine (FSM) can be constructed that accepts exactly the strings in the language.

Regular languages have a fundamental limitation: they cannot express patterns that require counting or matching of arbitrarily nested structures. The language { | } — equal numbers of 0s followed by equal numbers of 1s — is not regular. No regular expression can describe it, and no FSM can accept it.

LanguageRegular?Reason
Strings over {a, b} ending in 'ab'YesRE: (a|b)*ab
Binary strings with an even number of 1sYesCan be described by an FSM with 2 states
Equal numbers of a's followed by b'sNoRequires counting — no finite number of states suffices
All strings over {0,1}YesRE: (0|1)*

A language is regular if and only if it can be represented by a regular expression — and regular expressions and FSMs are equivalent ways of defining regular languages.

Regular Expressions and Finite State Machines

Regular expressions and FSMs are two different notations for the same class of languages — every regular expression has a corresponding FSM that accepts exactly the same strings, and vice versa.

Converting a simple FSM to a regular expression:

FSM over {a, b} with states (start), , (accept):

  • on 'a' → ; on 'b' →
  • on 'a' → ; on 'b' →
  • on 'a' → ; on 'b' →

This FSM accepts strings that end in 'ab'. The regular expression is: (a|b)*ab

Verify: (a|b)* matches any prefix; ab matches the final two characters. Any string ending in 'ab' is accepted. ✓

Converting a regular expression to an FSM:

RE: a*b — any number of a's followed by exactly one b.

FSM:

  • (start): on 'a' → (self-loop); on 'b' →
  • (accept): no further transitions (or any input leads to a dead/reject state)

State transition table:

StateInput aInput b
(start)
(accept)rejectreject

For AQA exam questions, you may be asked to: write the RE for a given FSM, draw the FSM for a given RE, or determine whether a specific string is accepted by a given RE or FSM. All three require the same underlying understanding of what strings the expression/machine accepts.

Common Exam Mistakes

1. Confusing * with +

a* matches the empty string; a+ does not. If the question says "one or more", use +. If zero occurrences are allowed, use *. Applying the wrong quantifier generates a different (usually larger) set than intended.

2. Forgetting that * and + apply to only the immediately preceding element

ab* means "a followed by zero or more b's" — not "zero or more copies of ab". To repeat the pair, write (ab)*. This is the most common grouping error.

3. Testing a string against a RE incorrectly

To check whether a string matches, parse the RE left to right and check whether the string can be consumed character by character following the pattern. Work through the string methodically — do not judge by inspection.

4. Writing (a|b)* when a simpler form exists

(a|b)* means "any string over {a, b}". This is often a correct component of a longer RE, but students sometimes write it when the intended language is more restricted. Read the description carefully before writing the full expression.

5. Claiming all pattern-matching is done with regular expressions

Regular expressions can only describe regular languages. Patterns with balanced brackets, matching open/close tags, or equal numbers of two symbols cannot be expressed with the five metacharacters above — they require more powerful formalisms such as context-free grammars (BNF).

Key terms

Regular expression
A compact notation that describes a set of strings (a language) by defining a pattern that member strings match.
Metacharacter
One of the special characters `*`, `+`, `?`, `|`, `( )` that AQA assesses, used to build patterns rather than match itself.
Literal
Any character that is not a metacharacter, which matches itself exactly.
Star (Kleene star) `*`
Matches zero or more repetitions of the preceding element, including the empty string.
Plus `+`
Matches one or more repetitions of the preceding element, excluding the empty string.
Question mark `?`
Makes the preceding element optional by matching zero or one repetition.
Pipe (alternation) `|`
Matches either the left or the right alternative, acting as a logical OR.
Parentheses `( )`
Group a sub-expression so an operator applies to the whole group rather than a single element.
Empty string (ε)
The string with zero symbols, matched by `*` but not by `+`.
Language
Any set of strings over a given alphabet.
Regular language
A language that can be described by a regular expression, equivalently one for which a finite state machine accepting exactly its strings can be built.
Finite state machine (FSM)
A machine equivalent to a regular expression that accepts exactly the strings of a regular language.

Frequently asked questions

`*` means zero or more repetitions of the preceding element, so it includes the empty string. `+` means one or more repetitions, so it requires at least one occurrence and does not match the empty string. If the question says 'one or more', use `+`.

It matches one literal 'a' followed by zero or more repetitions of either 'a' or 'b', so it generates the set {a, aa, ab, aaa, aab, aba, …}: any string that starts with 'a' followed by any sequence of a's and b's. It does not match b, ba or the empty string.

No. Regular expressions can only describe regular languages. Patterns with balanced brackets, matching open/close tags, or equal numbers of two symbols such as {0ⁿ1ⁿ | n ≥ 1} cannot be expressed with the five metacharacters and require more powerful formalisms such as context-free grammars (BNF).

Generate revision on any topic you study

Type any topic you're studying and Aicademy generates a complete lesson, quiz, and flashcard set, personalised to your level.

Lessons on anything

Structured, level-matched lessons on any topic you study

Practice quizzes

Find out what you actually know before the exam does

Flashcard sets

Lock in key concepts with instant revision cards

Ask Aica

Stuck on something? Get a clear explanation, any time

Prev

Sets and Formal Languages

Next

BNF and Syntax Diagrams

Related lessons

9 min

5 min

6 min

Top students don’t revise more. They revise what counts.

Start revising free

Free to start. No card needed.