Many useful languages are infinite, so we cannot list them. A regular expression is a short formula that describes such a language exactly. The same idea, with extra conveniences, is used in every programming language (grep, Python’s re, JavaScript’s /…/) to search and validate text.
Definition 2.9 · Regular expression
The regular expressions over an alphabet Σ \Sigma Σ are defined by these rules:
∅ \emptyset ∅ , λ \lambda λ , and each symbol a ∈ Σ a \in \Sigma a ∈ Σ are regular expressions;
if r r r and s s s are regular expressions, then so are ( r ) (r) ( r ) , r s rs r s (concatenation), r + s r + s r + s (union) and r ∗ r^* r ∗ (Kleene star);
nothing else is a regular expression.
r + r^+ r + is used as a shorthand for r r ∗ rr^* r r ∗ . Every regular expression r r r describes a language L ( r ) L(r) L ( r ) :
Expression Language L L L ∅ \emptyset ∅ ∅ \emptyset ∅ λ \lambda λ { λ } \{\lambda\} { λ } a a a { a } \{a\} { a } r s rs r s L ( r ) L ( s ) L(r)L(s) L ( r ) L ( s ) (product)r + s r + s r + s L ( r ) ∪ L ( s ) L(r) \cup L(s) L ( r ) ∪ L ( s ) r ∗ r^* r ∗ L ( r ) ∗ L(r)^* L ( r ) ∗
Precedence: ∗ ^* ∗ is applied first, then concatenation, then + + + . So a b ∗ + c ab^* + c a b ∗ + c means ( a ( b ∗ ) ) + c (a(b^*)) + c ( a ( b ∗ )) + c .
Note
In programming tools the union is written with a bar: the regular expression ( a + b ) ∗ a b b (a + b)^*abb ( a + b ) ∗ abb is written (a|b)*abb in Python, Java or grep -E.
Reading regular expressions
Regular expression over { 0 , 1 } \{0, 1\} { 0 , 1 } or { a , b } \{a, b\} { a , b } Language ( 0 + 1 ) ∗ (0 + 1)^* ( 0 + 1 ) ∗ all strings over { 0 , 1 } \{0, 1\} { 0 , 1 } 0 ∗ 10 ∗ 10 ∗ 0^*10^*10^* 0 ∗ 1 0 ∗ 1 0 ∗ strings with exactly two 1s ( 0 + 1 ) ∗ 11 (0 + 1)^*11 ( 0 + 1 ) ∗ 11 strings ending in 11 11 11 a b ∗ a ab^*a a b ∗ a strings that start and end with a a a with only b b b ‘s between: a a , a b a , a b b a , … aa, aba, abba, \dots aa , aba , abba , … a ∗ b ∗ a^*b^* a ∗ b ∗ all a a a ‘s (if any) come before all b b b ‘s (if any) ( a + b ) ∗ a ( a + b ) ∗ (a + b)^*a(a + b)^* ( a + b ) ∗ a ( a + b ) ∗ strings containing at least one a a a
Correction · The null string in the language of ab*a (Example 9)
Study material says: Example 9 (§2.2.3) lists the language of a b ∗ a ab^*a a b ∗ a as { ∧ , a a , a b a , a b b a , … } \{\wedge, aa, aba, abba, \dots\} { ∧ , aa , aba , abba , … } .
Correct version: L ( a b ∗ a ) = { a a , a b a , a b b a , a b b b a , … } = { a b n a ∣ n ≥ 0 } L(ab^*a) = \{aa, aba, abba, abbba, \dots\} = \{ab^na \mid n \geq 0\} L ( a b ∗ a ) = { aa , aba , abba , abbba , … } = { a b n a ∣ n ≥ 0 } . It does not contain the null string.
Why: Every string of L ( a b ∗ a ) L(ab^*a) L ( a b ∗ a ) begins with a a a and ends with a a a , so it has length at least 2; the text’s own description (“strings that begin and end with a’s”) excludes ∧ \wedge ∧ .
Intuition
Reading a regular expression is like reading a recipe: concatenation says “then”, + + + says “or”, and ∗ ^* ∗ says “repeat as often as you like, possibly zero times”. ( 0 + 1 ) ∗ 11 (0 + 1)^*11 ( 0 + 1 ) ∗ 11 reads: “anything, then 1, then 1”.
Example 2.10 Writing regular expressions for languages
Goal. Over { a , b } \{a, b\} { a , b } , write regular expressions for: (a) strings of even length; (b) strings containing the substring a b a aba aba ; (c) { λ , a , a b b , a b b b b , … } \{\lambda, a, abb, abbbb, \dots\} { λ , a , abb , abbbb , … } .
Step. (a) Even length = zero or more blocks of two letters: ( ( a + b ) ( a + b ) ) ∗ ((a + b)(a + b))^* (( a + b ) ( a + b ) ) ∗ , which equals ( a a + a b + b a + b b ) ∗ (aa + ab + ba + bb)^* ( aa + ab + ba + bb ) ∗ .
Step. (b) Anything, then a b a aba aba , then anything: ( a + b ) ∗ a b a ( a + b ) ∗ (a + b)^*aba(a + b)^* ( a + b ) ∗ aba ( a + b ) ∗ .
Step. (c) The strings are λ \lambda λ , or a a a followed by an even number of b b b ‘s: λ + a ( b b ) ∗ \lambda + a(bb)^* λ + a ( bb ) ∗ .
Step. Check (c): n = 0 , 1 , 2 n = 0, 1, 2 n = 0 , 1 , 2 give a a a , a b b abb abb , a b b b b abbbb abbbb ; the λ \lambda λ term gives λ \lambda λ .
Result. (a) ( ( a + b ) ( a + b ) ) ∗ ((a + b)(a + b))^* (( a + b ) ( a + b ) ) ∗ ; (b) ( a + b ) ∗ a b a ( a + b ) ∗ (a + b)^*aba(a + b)^* ( a + b ) ∗ aba ( a + b ) ∗ ; (c) λ + a ( b b ) ∗ \lambda + a(bb)^* λ + a ( bb ) ∗ .
Correction · Answers to Ex.3 and Ex.6(c)
Study material says: In §2.7, the answers to Ex.3 are “(a) a+b+c, (b) ab*+ba*, (c) ∧+a(bb)*”, although Ex.3 has only parts (a) and (b). The answer to Ex.6(c) gives the language of a ∗ b c ∗ + a c a^*bc^* + ac a ∗ b c ∗ + a c as { a , b , a b , b c , a b b , b c c , … , a b n , b c n , … } \{a, b, ab, bc, abb, bcc, \dots, ab^n, bc^n, \dots\} { a , b , ab , b c , abb , b cc , … , a b n , b c n , … } .
Correct version: Ex.3(b), { ∧ , a , a b b , a b b b b , … } \{\wedge, a, abb, abbbb, \dots\} { ∧ , a , abb , abbbb , … } , is described by ∧ + a ( b b ) ∗ \wedge + a(bb)^* ∧ + a ( bb ) ∗ (the printed “(c)”); a b ∗ + b a ∗ ab^* + ba^* a b ∗ + b a ∗ belongs to no part of the question. L ( a ∗ b c ∗ + a c ) = { a c } ∪ { a i b c j ∣ i , j ≥ 0 } = { b , a b , a c , b c , a a b , a b c , b c c , … } L(a^*bc^* + ac) = \{ac\} \cup \{a^ibc^j \mid i, j \geq 0\} = \{b, ab, ac, bc, aab, abc, bcc, \dots\} L ( a ∗ b c ∗ + a c ) = { a c } ∪ { a i b c j ∣ i , j ≥ 0 } = { b , ab , a c , b c , aab , ab c , b cc , … } .
Why: Every string of a ∗ b c ∗ a^*bc^* a ∗ b c ∗ has exactly one b b b , with a a a ‘s only before it and c c c ‘s only after it. So a a a (no b b b , and not a c ac a c ), a b b abb abb and a b n ab^n a b n for n ≥ 2 n \geq 2 n ≥ 2 (more than one b b b ) are not in the language. A program listing all strings up to length 5 confirms this.
Common mistake
Writing a ∗ b ∗ a^*b^* a ∗ b ∗ for “any mix of a a a ‘s and b b b ‘s”. a ∗ b ∗ a^*b^* a ∗ b ∗ does not contain b a ba ba . All strings over { a , b } \{a, b\} { a , b } are ( a + b ) ∗ (a + b)^* ( a + b ) ∗ .
Common mistake
Forgetting the star for “any number of times”. “Even length” is ( ( a + b ) ( a + b ) ) ∗ ((a + b)(a + b))^* (( a + b ) ( a + b ) ) ∗ , not ( a a + a b + b a + b b ) (aa + ab + ba + bb) ( aa + ab + ba + bb ) , which has only four strings of length 2.
Key idea
A regular expression builds a language from single symbols using union (+ + + ), concatenation and Kleene star; reading it as “or”, “then” and “repeat” gives its meaning.
TEE Exam note
Two kinds of questions: “Describe the language of …” (give a sentence and the first few strings) and “Write a regular expression for …” (test it on two strings in the language and one outside). Answers: Ex.3(a) a + b + c a + b + c a + b + c ; Ex.4 binary numerals without leading zeros: 0 + 1 ( 0 + 1 ) ∗ 0 + 1(0 + 1)^* 0 + 1 ( 0 + 1 ) ∗ ; Ex.6(a) { a , b } \{a, b\} { a , b } , Ex.6(b) { a , λ , b , b b , b b b , … } \{a, \lambda, b, bb, bbb, \dots\} { a , λ , b , bb , bbb , … } ; Ex.7(a) ( ( a + b ) ( a + b ) ) ∗ ((a + b)(a + b))^* (( a + b ) ( a + b ) ) ∗ , Ex.7(b) ( a + b ) ∗ a b a ( a + b ) ∗ (a + b)^*aba(a + b)^* ( a + b ) ∗ aba ( a + b ) ∗ .
Key takeaways
Regular expressions are built from ∅ \emptyset ∅ , λ \lambda λ and symbols with + + + , concatenation and ∗ ^* ∗ .
Precedence: star, then concatenation, then union.
L ( r + s ) = L ( r ) ∪ L ( s ) L(r + s) = L(r) \cup L(s) L ( r + s ) = L ( r ) ∪ L ( s ) , L ( r s ) = L ( r ) L ( s ) L(rs) = L(r)L(s) L ( r s ) = L ( r ) L ( s ) , L ( r ∗ ) = L ( r ) ∗ L(r^*) = L(r)^* L ( r ∗ ) = L ( r ) ∗ .
Test every regular expression on strings that should and should not be in the language.
Practice questions Answer on paper first, then check. The explanation matters more than the letter you chose.
Question 1 · multiple choice Which string belongs to L ( 0 ∗ 10 ∗ 10 ∗ ) L(0^*10^*10^*) L ( 0 ∗ 1 0 ∗ 1 0 ∗ ) ?
Check answer
Show answer Answer: B. 0010100 0010100 0010100
The language is strings with exactly two 1s; 0010100 0010100 0010100 has two. Option 1 has four 1s and option 3 only one.
Question 2 · multiple choice Which regular expression describes all strings over { a , b } \{a, b\} { a , b } that end in b b b ?
Check answer
Show answer Answer: C. ( a + b ) ∗ b (a + b)^*b ( a + b ) ∗ b
Anything followed by a final b b b . Option 2 misses strings like b a b bab bab ; option 4 describes strings that start with b b b .
Question 3 · multiple choice Which string is NOT in L ( a ∗ b ∗ ) L(a^*b^*) L ( a ∗ b ∗ ) ?
Check answer
Show answer Answer: D. a b a aba aba
In a ∗ b ∗ a^*b^* a ∗ b ∗ no a a a may come after a b b b . λ \lambda λ is in the language (zero a a a ‘s and zero b b b ‘s).
Question 4 · multiple choice What does a b ∗ + c ab^* + c a b ∗ + c mean, by the precedence rules?
Check answer
Show answer Answer: A. ( a ( b ∗ ) ) + c (a(b^*)) + c ( a ( b ∗ )) + c
Star binds tightest, then concatenation, then + + + . Option 2 would star the whole a b ab ab .
Question 5 · multiple choice Which expression describes strings over { a , b } \{a, b\} { a , b } with at least one a a a ?
Check answer
Show answer Answer: D. ( a + b ) ∗ a ( a + b ) ∗ (a + b)^*a(a + b)^* ( a + b ) ∗ a ( a + b ) ∗
Some string, then an a a a , then some string. Option 2 also contains b b b and λ \lambda λ , which have no a a a .
Question 6 · short answer
Write a regular expression for binary numerals without leading zeros (except 0 itself), and check it on 0 0 0 , 101 101 101 and 011 011 011 .
Show worked answer 0 + 1 ( 0 + 1 ) ∗ 0 + 1(0 + 1)^* 0 + 1 ( 0 + 1 ) ∗ . 0 0 0 matches the first alternative. 101 101 101 = 1 1 1 followed by 01 ∈ L ( ( 0 + 1 ) ∗ ) 01 \in L((0 + 1)^*) 01 ∈ L (( 0 + 1 ) ∗ ) , so it matches the second. 011 011 011 starts with 0 but is not exactly “0”, so it matches neither alternative — correctly rejected.