Speak Maths Β· University
Regular expressions: patterns of text
A regular expression is a tiny language that describes a whole family of strings at once. Programmers use it every day; mathematicians ask which strings a pattern lets through.
Draft: written by an AI helper and not yet checked by a teacher.
The alphabet
Read each sign aloud, the way it is written under it.
- Say: c, a, t, just as written; and c, dot, t: a dot is any one characterMeans: Letters stand for themselves; a dot stands for any single character.
- Say: a star: a, any number of times, even none; a plus: at least once; a question mark: once or not at allMeans: Counts how often the piece before it may repeat.
- Say: square brackets a b c: one character from the list; a dash z: one from the range; a caret first: one character not in the listMeans: One character, taken from a chosen set.
- Say: caret, a, b, dollar: the string starts here, says a b, and ends hereMeans: Pins the pattern to the start and to the end of the string.
- Say: in brackets: a b, or c; all of that, one or more timesMeans: A bar chooses between pieces; brackets group pieces so that a count applies to all of them.
- Say: a, between two and four timesMeans: Says exactly how many repeats are allowed: a number, or from and to.
- Say: backslash d: one digit; backslash w: one letter, digit or underscore; backslash s: one spaceMeans: Short names for the commonest classes: digits, word characters, spaces.
- Say: s is in L of r, which sits inside sigma starMeans: L of r is the set of strings that r accepts as a whole; sigma star is the set of all strings over the alphabet Ξ£.
The story: The gatekeeper
Tap a line to see its words.
- The gatekeeperβs rule r: a code is one letter, A or B, then one digit, and nothing more.
- The code A7 fits the rule: it is in the language of r.
- The code C7 does not: C is not in the class.
- A77 is turned away: the end anchor insists that the code stops after one digit.
- Visitors complain, so a plus allows one or more digits. Call the new rule rβ².
- Now A77 is let in.
- For visiting hours she checks times: two digits, a colon, two digits. Call this rule d.
- The time 09:30 passes.
- The time 9:30 does not: it has one digit where two are required.
- Whatever r lets in, rβ² lets in too; and both judge only strings over the alphabet.
In words this story is 624 letters long; in symbols, 99.
Read it
What does it say?
Write it
Which notation says this?
A code is a letter a, b or c followed by exactly three digits, and nothing else.
The whole string is either cat or dog.
The string s is accepted by the pattern r.
A locker code: exactly two capital letters, then exactly two digits, and nothing else.
The word colour, where the u is optional.
The whole string is the pair ab repeated one or more times.
Between two and four aβs, then a single b, and nothing else.
The pattern r accepts no string at all.
The string s is not accepted by the pattern r.
Every string that r accepts, rβ² accepts too.
A word: one or more letters, digits or underscores, and nothing else.
The whole string is yes, or no, or empty.
Build it
Tap the tiles in order. One tile does not belong.
A time of day: the string starts, two digits, a colon, a minute digit from 0 to 5, any digit, the string ends.
The string s is accepted by r, and everything r accepts is a string over the alphabet.
A seat code: the string starts, one capital letter from A to Z, exactly three digits, the string ends.
The whole string is the pair ab one or more times, then an optional c.
The string s is accepted by r but not by rβ²: it lies in the language of r minus the language of rβ².
A room label: the string starts, one or more small letters, one space, one or more digits, the string ends.
Challenges
Creative puzzles that use every sign of this chapter. Take your time; there is no clock.
1 The gatekeeperβs grid
Four rules (columns), five codes (rows). The letter s runs over the five codes and r over the four rules. Judge each sentence in this grid.
| β | β | β | ||
| β | ||||
| β | ||||
| β | β | |||
A tick means the code s is accepted, as a whole, by the rule r.
The language of rβ lies inside that of rβ, so rβ never accepts what rβ refuses; the converse fails, and B12 shows it. And one code, AB, is turned away by every rule.
2 Look-alike patterns
Four patterns that differ by a bracket or a star. Pair each with what it accepts (whole strings, made of a and b).
Tap a line, then its meaning.
A star binds only to the piece just before it: a single letter, a bracketed pair, or a bracketed choice. So the four patterns repeat a letter, a pair, a choice, and two letters in turn.
3 The wrong digit count
This pattern should accept a year of exactly four digits, a dash and a month of two digits, like 2026-10 (the whole string). One tile is wrong. Find it, then fix it.
Tap the tile that is wrong.
Three digits would reject every four-digit year. A plain plus, or a range of three to four, would also let shorter years through; only the count four says exactly four.
4 Which string gets in?
The gate has this rule: a pair ab or a single c, then at most one digit, and nothing more. Which of the three strings does it accept?
Brackets make ab one piece, so the string with ab and then a digit fits. The one with abc has a c after ab, which the rule does not allow, and the one with two cβs has two pieces where only one is allowed.
5 How many get in?
The rule accepts only strings over the three letters a, b and c. How many strings does the language of this pattern contain?
Three places, each with two choices (a or b): 2 Γ 2 Γ 2 = 8.
6 The pattern that lets everyone in
What is the language of the pattern dot-star?
A dot is any one character and the star allows any number of them, even none: every string, including the empty one, so Ξ£*.
7 Write the time-checker
A clock string looks like 09:30: hours from 00 to 23, a colon, minutes from 00 to 59. Write a pattern that accepts exactly such strings, then compare.
One way:
Hours need a choice: 0 or 1 then any digit, or 2 then 0 to 3. Minutes: 0 to 5, then any digit. Anchors keep the check on the whole string. A looser pattern with two plain pairs of digits also accepts hours above 23 and minutes above 59.
8 Say it as a set
Write, as a set, the language of the pattern cat|dog.
One way:
A bar offers two whole alternatives, so the language has exactly two strings: cat and dog. (Unanchored, a search would also find them inside longer strings; as a language we count whole strings.)
9 Five plates, four rules
Four rules for a number plate (columns) and five plates (rows). The letter s runs over the plates and p over the rules. Judge each sentence in this grid.
| β | β | β | ||
| β | β | |||
| β | β | β | ||
| β | β | β | ||
| β |
A tick means the plate s is accepted, as a whole, by the rule p.
A letter then digits is a special case of letters then digits, so pβ sits inside pβ. Word characters allow digits first, which pβ forbids: that is the plate starting with 12. Every plate passes some rule, since backslash-w takes them all.
10 Password rules
Four password checks (columns): at least eight characters; only word characters; contains a digit; only small letters. Five passwords (rows). Judge each sentence in this grid.
| β | β | |||
| β | β | β | ||
| β | β | β | ||
| β | β | β | ||
| β |
A tick means the password s passes the check q.
Dot with a count of eight or more is a length check. No password is both all small letters and digit-bearing: the two classes exclude each other. Only the password with a space fails the word-character check, and no password passes all four, since the lowercase-only rule fights the digit rule.
11 How many aβs?
Five ways to count the letter a. Pair each with what it allows (whole strings).
Tap a line, then its meaning.
The question mark and the star both allow zero; the plus and the braces do not. Braces with one number mean exactly that many; a trailing comma leaves the upper end open.
12 One character, which one?
Five patterns for a character or a few. Pair each with what it accepts.
Tap a line, then its meaning.
A range in brackets is the same as backslash-d; a caret inside the brackets flips it. The plus adds repetition, the dot drops all restriction, and backslash-s is the space.
13 A number, not nothing
This pattern should accept a whole string of one or more digits. As written it also accepts the empty string. One tile is wrong. Find it, then fix it.
Tap the tile that is wrong.
A star allows zero repeats, so the empty string gets in. The plus demands at least one digit. A question mark would allow at most one digit, and a count of one exactly one.
14 One letter, not three
A code is one of the letters a, b or c, then exactly two digits, and nothing else. This pattern asks for all three letters in a row. One tile is wrong. Find it, then fix it.
Tap the tile that is wrong.
Brackets group: the string must contain abc. Square brackets choose one character from the list. An optional c still demands ab, and a plus after the range would allow many letters.
15 Who is turned away?
The rule accepts any whole string made of aβs and bβs, even none. Which of the three strings is not accepted?
A star allows zero letters, so the empty string Ξ΅ gets in; abba is all aβs and bβs. Only the string with a c has a character outside the class.
16 How many two-digit strings?
The rule accepts a whole string of exactly two digits. How many strings does its language contain?
Two places, ten digits each: 10 Γ 10 = 100. The string 07 counts too; as strings, leading zeros are allowed, so it is not 90.
17 Write the plate rule
A plate is two capital letters, a dash, then three digits, and nothing else. Write the pattern, then compare.
One way:
A range of capitals with a count of two, the dash as itself, three digits by count or written out, and anchors at both ends so nothing extra sneaks in.
18 Even in binary
A binary number is even exactly when its last digit is 0. Write a pattern that accepts exactly the binary strings of even numbers, then compare.
One way:
Any run of 0βs and 1βs, then a final 0, anchored at both ends. The star may be empty, so the single digit 0 is accepted, and 1 or the empty string is not.
Chapter finished. The symbols are yours.
Tell your own
Invent a rule for a gate (a password, a plate, a door code) and write it as a pattern, then one string it lets in and one it keeps out.
Kept on this device only.