AQA 7517, section 4.4.2.3
A regular expression is simply a way of describing a set of strings. It lets particular types of language be described in a convenient shorthand notation.
For example, the regular expression a(a|b)* generates the set of strings : every string that starts with a, followed by any mix of a and b.
Regular expressions are used for string matching and manipulation: validating an email address or postcode, searching text, and find-and-replace.
Metacharacters
A metacharacter has a special meaning. Ordinary characters match themselves.
| Metacharacter | Meaning | Example | Matches |
|---|---|---|---|
* |
0 or more repetitions | ab* |
a, ab, abb, abbb, ... |
+ |
1 or more repetitions | ab+ |
ab, abb, abbb, ... (not a) |
? |
0 or 1 (optional) | ab? |
a, ab |
| |
Alternation (or) | a|b |
a or b |
( ) |
Group part of the expression | (ab)+ |
ab, abab, ababab, ... |
Brackets control what a repetition applies to. ab* means a followed by any number of bs, but (ab)* means any number of the pair ab.
Any other metacharacters used in an exam question will be explained in the question.
Building expressions
Concatenation (writing things next to each other) means one after another: abc matches only the string abc.
| Expression | Strings it matches |
|---|---|
cat |
cat |
cat|dog |
cat, dog |
colou?r |
color, colour |
(0|1)+ |
any non-empty string of 0s and 1s |
a(a|b)* |
any string starting with a, then as and bs |
(ab)* |
the empty string, ab, abab, ... |
0*10* |
strings with exactly one 1, such as 1, 010, 0001 |
In Python
Python's re module uses the same notation.
import re
pattern = r"a(a|b)*"
print(bool(re.fullmatch(pattern, "abba"))) # True
print(bool(re.fullmatch(pattern, "bab"))) # False: starts with b
print(bool(re.fullmatch(r"colou?r", "color"))) # True
print(bool(re.fullmatch(r"(0|1)+", "10110"))) # True
print(bool(re.fullmatch(r"(0|1)+", ""))) # False: needs 1+
fullmatch checks that the whole string matches, which is how a regular expression is normally understood in this topic.
Regular expressions and FSMs
Regular expressions and finite state machines are equivalent ways of defining a regular language (4.4.2.4). For any regular expression there is an FSM that accepts exactly the same strings, and for any FSM there is a regular expression that describes the strings it accepts.
You should be able to write a regular expression for the same language as a given FSM, and the other way round.
From an expression to an FSM
The expression ab* (an a followed by any number of bs):
| Current state | Input a | Input b |
|---|---|---|
| Q0 (start) | Q1 | Dead |
| Q1 (accepting) | Dead | Q1 |
| Dead | Dead | Dead |
Dead is a state it can never leave, for any input that can't lead to acceptance. The machine accepts a, ab, abb, but rejects b or aa.
From an FSM to an expression
The "even number of 1s" machine from 4.4.2.1 can be described by:
In 0*(10*10*)*: any 0s, then any number of groups made of a 1, some 0s, a second 1, and some more 0s. Each group adds two 1s, so the total number of 1s is always even.
Worked example
Task: write a regular expression for a simple UK-style postcode made of two letters, one digit, a space, one digit and two letters (such as AB1 2CD).
Using [A-Z] for "any capital letter" and [0-9] for "any digit":
import re
pattern = r"[A-Z][A-Z][0-9] [0-9][A-Z][A-Z]"
print(bool(re.fullmatch(pattern, "AB1 2CD"))) # True
print(bool(re.fullmatch(pattern, "AB12CD"))) # False: no space
The square brackets are a shorthand: [A-Z] is the same as writing A|B|C|...|Z. In an exam, any such extra notation will be explained in the question.
Exam tip: be careful with what each metacharacter applies to. * and + apply to the single item or bracketed group just before them. Test your expression on a couple of strings that should match and a couple that shouldn't.
Check yourself
What does the regular expression ab+ match?
An a followed by one or more bs: ab, abb, abbb and so on.
Write a regular expression for strings of 0s and 1s that end in 1
(0|1)*1.
What is the relationship between regular expressions and FSMs?
They are equivalent ways of defining a regular language. Each regular expression has an FSM that accepts the same strings, and each FSM has a regular expression for them.
What does colou?r match?
color and colour, because the u is optional.