Regular Expression Examples

Example 1: Let Σ = {a, b}. Write regular expression to define language consisting of strings w such that,

w contains only a’s or only b’s of length zero or more.

Solution:  r = a* + b*

Example 2: Let Σ = {a, b}. Write regular expression to define language consisting of strings w such that, w is of length one or more and contains only a’s or only b’s. r = a+ + b+

Solution:  r = a++ b+

Example 3: Let Σ = {a, b}. Write regular expression to define language consisting of strings w such that, w contains zero or more a’s followed by zero or more b’s

Solution: r = a*b*

Example 4: Let Σ = {a, b}. Write regular expression to define language consisting of strings w such that, w of length even 

Solution: r = [(a + b) (a + b)]*

Example 5: Let Σ = {a, b}. Write regular expression to define language consisting of strings w such that, w of length odd 

Solution: r = (a + b) [(a + b) (a + b)]*

Example 6: Let Σ = {a, b}. Write regular expression to define language consisting of strings w such that, w of length three 

Solution: r = (a + b) (a + b) (a + b)

Example 7: Let Σ = {a, b}. Write regular expression to define language consisting of strings w such that, w of length atmost three 

Solution:  r = (a + b + ∈) (a + b + ∈) (a + b + ∈)

Example 8: Let Σ = {a, b}. Write regular expression to define language consisting of strings w such that, w of length odd containing only b’s 

Solution: r = (bb)* b

Example 9: Let Σ = {a, b}. Write regular expression to define language consisting of strings w such that, w starting with a always 

Solution: r = a(a + b)*

Example 10: Let Σ = {a, b}. Write regular expression to define language consisting of strings w such that, w starting and ending with b and having only a’s in between.

Solution:  r = b a* b

Example 11: Let Σ = {a, b}. Write regular expression to define language consisting of strings w such that, w starting and ending with same double letter 

Solution: r = {(aa (a + b)* aa) | (bb (a + b)* bb)

Example 12: Let Σ = {a, b}. Write regular expression to define language consisting of strings w such that, w with starting and ending with different letters 

Solution: r = (a(a+b)* b) | (b (a + b)* a)

Example 13: Let Σ = {a, b}. Write regular expression to define language consisting of strings w such that, w with at least two occurrence of a 

Solution: r = (a + b)* a (a + b)* a (a + b)*

Example 14: Let Σ = {a, b}. Write regular expression to define language consisting of strings w such that, w with exactly two occurrence of a

Solution:  r = b* a b* a b*

Example 15: Let Σ = {a, b}. Write regular expression to define language consisting of strings w such that, w with at most two occurrence of a 

Solution: r = b* (a + ∈) b* (a + ∈) b*

Example 16: Let Σ = {a, b}. Write regular expression to define language consisting of strings w such that, w with begin or end with aa or bb 

Solution: r = ((aa + bb) (a + b)*) + ((a + b) * (aa + bb))

Example 17: Let Σ = {a, b}. Write regular expression to define language consisting of strings w such that, w with begin and end with aa or bb 

Solution: r = ((aa + bb) (a + b)* (aa + bb)) + aa + bb

Example 18: Let Σ = {a, b}. Write regular expression to define language consisting of strings w such that, w with total length multiple of 3 always 

Solution: r = [(a + b) (a + b) (a + b)]*

Example 19: Let Σ = {a, b}. Write regular expression to define language consisting of strings w such that, w containing total a’s as multiple of 3 always 

Solution: r = [b* a b* a b* a b*]*

Example 20: Let Σ = {a, b}. Write regular expression to define language consisting of strings w such that, w with exactly two or three b’s 

Solution: r = a* b a* b a* (b + ∈) a*

Example 21: Let Σ = {a, b}. Write regular expression to define language consisting of strings w such that, w with number of a’s even 

Solution: r = b* + (b* a b* a b*)*

Example 22: Let Σ = {a, b}. Write regular expression to define language consisting of strings w such that, w in which b is always tripled 

Solution: r = (a + bbb)*

Example 23: Let Σ = {a, b}. Write regular expression to define language consisting of strings w such that, w with at least one occurrence of substring aa or bb

Solution:  r = (a + b)* (aa + bb) (a + b)*

Example 24: Let Σ = {a, b}. Write regular expression to define language consisting of strings w such that, w with at the most one occurrence of sub-string bb 

Solution: r = (a + ba)* (bb + ∈) (a + ab)*

Example 25: Let Σ = {a, b}. Write regular expression to define language consisting of strings w such that, w without sub-string ab 

Solution: r = b* a*

Example 26: Let Σ = {a, b}. Write regular expression to define language consisting of strings w such that, w without sub-string aba 

Solution: r = (a + ∈) (b + aa+ )* (a + ∈)

Example 27: Let Σ = {a, b}. Write regular expression to define language consisting of strings w such that, w in which 3rd character from right end is always a 

Solution: r = (a + b)* a (a + b) (a + b)

Example 28: Let Σ = {a, b}. Write regular expression to define language consisting of strings w such that, w always start with ‘a’ and the strings in which each ‘b’ is preceded by ‘a’.

Solution: (a + ab)*

Example 29: Let Σ = {a, b}. Write regular expression to define language consisting of strings w such that, w contains atleast one ‘a’.

Solution: (a + b)* a (a + b)*

Example 30: Let Σ = {a, b}. Write regular expression to define language consisting of strings w such that, w contain atleast two ‘a’s or any number of ‘b’s.

Solution: (a* a b* a b*) + b*

Example 31: Let Σ = {a, b}. Write regular expression to define language consisting of strings w such that, w contain atleast one ‘a’ followed by any number of ‘b’s followed by atleast one ‘c’.

Solution: a+ b* c+