You are viewing a free preview of this lesson.
Subscribe to unlock all 10 lessons in this course and every other course on LearningBro.
A regular expression (regex) is a concise notation for describing patterns in text. Regular expressions and finite state machines are mathematically equivalent — every regular expression can be converted to an FSM, and every FSM can be described by a regular expression. Together, they define the class of regular languages.
Regular expressions are used extensively in:
The A-Level specification requires you to understand the following operators:
| Operator | Name | Meaning | Example | Matches |
|---|---|---|---|---|
| (none) | Concatenation | One pattern followed by another | ab | "ab" |
| | | Alternation (OR) | One pattern or another | a|b | "a" or "b" |
| * | Kleene star | Zero or more repetitions | a* | "", "a", "aa", "aaa", ... |
| + | Kleene plus | One or more repetitions | a+ | "a", "aa", "aaa", ... |
| ? | Optional | Zero or one occurrence | a? | "" or "a" |
| () | Grouping | Group sub-expressions | (ab)* | "", "ab", "abab", ... |
So ab*|c means a(b*) | c — "a followed by zero or more b's" OR "c".
| Regex | Language described |
|---|---|
| a | {"a"} |
| ab | {"ab"} |
| a|b | {"a", "b"} |
| a* | {"", "a", "aa", "aaa", ...} |
| (a|b)* | All strings over {a, b}, including the empty string |
| a+b | {"ab", "aab", "aaab", ...} |
| (ab)+ | {"ab", "abab", "ababab", ...} |
| a(b|c)d | {"abd", "acd"} |
| (0|1)*01 | All binary strings ending in "01" |
| 1(0|1)*1 | All binary strings starting and ending with 1 (length ≥ 2) |
Kleene's Theorem states that the following are equivalent:
This means:
Subscribe to continue reading
Get full access to this lesson and all 10 lessons in this course.