Regular expression examples

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+

Recommended:

  1. Definition of DFA
  2. DFA notations
  3. How DFA process inputs
  4. DFA solved examples
  5. Definition of NFA
  6. Moore machine
  7. Mealy machine
  8. Regular expression examples
Python Programming ↓ 👆
Java Programming ↓ 👆
JAVA EasyExamNotes.com covered following topics in these notes.
JAVA Programs
Principles of Programming Languages ↓ 👆
Principles of Programming Languages
EasyExamNotes.com covered following topics in these notes.

Practicals:
Previous years solved papers:
A list of Video lectures References:
  1. Sebesta,”Concept of programming Language”, Pearson Edu 
  2. Louden, “Programming Languages: Principles & Practices” , Cengage Learning 
  3. Tucker, “Programming Languages: Principles and paradigms “, Tata McGraw –Hill. 
  4. E Horowitz, "Programming Languages", 2nd Edition, Addison Wesley

    Computer Organization and Architecture ↓ 👆

    Computer Organization and Architecture 

    EasyExamNotes.com covered following topics in these notes.

    1. Structure of desktop computers
    2. Logic gates
    3. Register organization
    4. Bus structure
    5. Addressing modes
    6. Register transfer language
    7. Direct mapping numericals
    8. Register in Assembly Language Programming
    9. Arrays in Assembly Language Programming

    References:

    1. William stalling ,“Computer Architecture and Organization” PHI
    2. Morris Mano , “Computer System Organization ”PHI

    Computer Network ↓ 👆
    Computer Network

    EasyExamNotes.com covered following topics in these notes.
    1. Data Link Layer
    2. Framing
    3. Byte count framing method
    4. Flag bytes with byte stuffing framing method
    5. Flag bits with bit stuffing framing method
    6. Physical layer coding violations framing method
    7. Error control in data link layer
    8. Stop and Wait scheme
    9. Sliding Window Protocol
    10. One bit sliding window protocol
    11. A protocol Using Go-Back-N
    12. Selective repeat protocol
    13. Application layer
    References:
    1. Andrew S. Tanenbaum, David J. Wetherall, “Computer Networks” Pearson Education.
    2. Douglas E Comer, “Internetworking with TCP/IP Principles, Protocols, And Architecture",Pearson Education
    3. KavehPahlavan, Prashant Krishnamurthy, “Networking Fundamentals”, Wiley Publication.
    4. Ying-Dar Lin, Ren-Hung Hwang, Fred Baker, “Computer Networks: An Open Source Approach”, McGraw Hill.