CS246 Homework 6: Regular Expressions

 

Due: Monday, April 2 2:30pm

What to hand in:

The power of regexes [50pts]

  1. Give the correct grep/egrep command with regular expression for the following patterns. For testing purposes, just make up some test file with appropriate data, you can also copy greptest from /home/dxu/handouts/cs246.
    1. lines with characters that are not letters or spaces
    2. lines with no vowels
    3. lines with exactly 9 characters
    4. lines with at least 9 characters
    5. lines with odd number of characters
    6. lines that end with two digits
    7. lines with 2 0s
    8. lines with 2 consecutive 0s
    9. lines with a 5 letter word
    10. lines with a word of at least 5 letters
    11. lines containing patterns that start and end with the same letter
    12. lines containing patterns that start and end with the same letter and with odd number of letters in between
    13. lines containing a repeated word of 2 letters separated by a space, i.e. "55 55"
    14. lines containing 9 consecutively repeated digits, i.e. "000000000", or "222222222"
    15. lines containing 3 repeated digits, not necessarily consecutive, i.e "3 3 3", "55 5", "666" or "a6b6c6d"
    16. lines with exactly 2 words
  2. In addition, write the regular expressions that generate the following sets, if possible. If not, give reasons why. Note that bitstrings are strings that consists of 0's and 1's only. Also, your expressions must be complete, meaning that it should generate all possible bitstrings that satisfy the given criteria, not just a subset.
    1. Bitstrings that begin with 0 and end with 1.
    2. Bitstrings that has 011010 as a substring.
    3. Bitstrings with equal number of 0’s and 1’s.
    4. Bitstrings that have no consecutive 1’s.
    5. Bitstrings that have an odd number of 0’s.