PyGCSE Python Lab

Regular Expression Tester

Write formal regular expressions and test them against input strings. Uses a proper NFA-based engine (not JavaScript regex) so the behaviour matches AQA exam notation. Spec reference: §4.4.2.

📖 Learn Step-by-Step

✓ Valid regex

Formal regex syntax:

a — literal characterAB — concatenationA|B — alternation (or)A* — Kleene star (0+)A+ — one or moreA? — optional (0 or 1)(A) — groupingε — empty string

Test Strings

"a"MATCH
"ab"MATCH
"abb"MATCH
"b"NO MATCH
"ba"NO MATCH
"aaa"MATCH
ε (empty)NO MATCH
"bab"NO MATCH

Regular Expression Practice Questions

Q1

The regular expression (a|b)*a matches all strings over {a,b} that end with 'a'. How many of these 8 strings does it match: a, b, aa, ab, ba, bb, aba, bba?

0/1 mark
Q2

How many distinct strings of length exactly 2 are in the language described by (0|1)(0|1)?

0/1 mark
Q3

The regex a*b matches strings of zero or more 'a's followed by exactly one 'b'. What is the length of the shortest string this regex matches?

0/1 mark
Q4

Given the regex (ab|cd)*, how many strings of length 4 belong to the language?

0/2 marks
Q5

A regular expression over {0,1} matches all binary strings containing '11' as a substring. It can be written as (0|1)*11(0|1)*. How many strings of length 3 does it match?

0/2 marks
Quick reference — Regular Expressions

What is a regular expression?

A regular expression (regex) is a pattern that describes a set of strings — a regular language. In formal CS (as opposed to programming regex), the operations are concatenation, alternation (|), and Kleene star (*).

Formal operators

abConcatenation — 'a' followed by 'b'
a|bAlternation — 'a' or 'b'
a*Kleene star — zero or more 'a's
a+One or more 'a's (shorthand for aa*)
a?Optional — zero or one 'a'
εThe empty string

Equivalence with FSMs

Every regular expression can be converted to an equivalent FSM (DFA or NFA), and vice versa. This tool uses Thompson's construction to build an NFA from the regex, then simulates it.

AQA exam tips

  • Operator precedence: * binds tightest, then concatenation, then |
  • Use brackets to clarify grouping
  • Remember: a* includes the empty string ε
  • You may be asked to write a regex, draw an equivalent FSM, or analyse which strings match