Regular Expression Extra Credit

Due Tuesday, 4/9 (late 6am 4/10)

Complete the Regular Expression quiz on Moodle.

The tutorial at the following link has good information about regular expressions. Some of the pages are specific to some flavors of regular expressions, but most will work in egrep. Regular Expression Tutorial

I have created the file that the tutorial references. I have modified the file from the original by adding some tab characters and carriage returns. To test out examples, run egrep on my file regex_tutorial_example_file.txt

egrep 'cat|dog' regex_tutorial_example_file.txt

For each of the following questions, the answer uses topics that are discussed in the tutorial section. The later questions may use information from previous sections. Limit your answers to techniques that are covered in the current section or previous sections in the order below. For example, the questions from the Character section can only use techniques covered in the Introduction and Character sections.

By default, egrep will echo all the lines that contain the regular expression. Add the -o switch to echo just the expressions that match the pattern. In the exercises below, you will be asked to display lines or display expressions. Use the appropriate call to egrep for each to test that your answers are correct.

egrep -o 'cat|dog' regex_tutorial_example_file.txt

You may attempt this quiz a maximum of three times, up until the due date. The grade for the quiz will count as an extra credit assignment.

I recommend that you create a bash script that calls egrep with a regular expression that can answer the question. Check your answers against the text file before answering the questions in the online quiz.

Questions that are on the quiz

regex.001.Introduction
Which of the following patterns form the file are matched by reg(ular expressions?|ex(p|es)?)
regex.002.Character - Question 1
How many lines in the file match the regular expression iz
regex.003.Character - Question 2
How many lines in the file will match regular expression (re
regex.004.Character - Question 3
Write the regular expression that will match the \ character. How many lines in the file contain the \ character?
regex.005.Character - Question 4
Write the regular expression that will match the tab character, \t . How many lines in the file contain the tab character? Note: The shell will automatically expand the tab character \t. To prevent this, write the tab character as $'\t'. This will prevent the shell from expanding the tab character. This will only work from a bash shell. Before trying this example, type bash at the command line to start a new bash shell.
regex.006.Optional Items
Write the regular expression that will match either editor or editors. Which words are in the file?
regex.007.Repetition - Question 1
Write the regular expression that will match a sequence of lower case letters that start with d and end with y. From the file, how many UNIQUE patterns match the regular expression?
regex.008.Repetition - Question 2
Write the regular expression that will match a sequence of lower case letters that start with t and end with s, and have from three to five letters separating them. Which of the following match the pattern and are in the file?
regex.009.Repetition - Question 3
The tutorial lists a regular expression for matching HTML tags. Closing tags start with </, opening tags do not. Write a regular expression that matches opening HTML tags from the file that are exactly six characters long, including the < and >. Which of the following match the pattern and are in the file?
regex.010.Repetition - Question 4
Write the regular expression that will match consecutive runs of at least six characters that are not spaces, digits, upper case or lower case letters. How many such sequences are in the file?
regex.011.Dot - Question 1
Write the regular expression that will match the letters d and s separated by exactly one other character (not just letters). From the file, how many UNIQUE patterns match the regular expression?
regex.012.Dot - Question 2 (True/False)
At least one line in the file will contain a string that will match the regular expression DOCTYPE.*Content
regex.013.Dot - Question 3 (True/False)
At least one line in the file will contain a string that will match the regular expression DOCTYPE.*CONTENT
regex.014.Dot - Question 4
Which of the following are in the file and are displayed as expressions that match the regular expression text.*file
regex.015.Dot - Question 5
Write the regular expression that will find all quoted strings longer than 70 characters. How many are in the file?
regex.016.Anchors - Question 1
Write the regular expression that will find all lines that start with uppercase T. How many lines start with uppercase T?
regex.017.Anchors - Question 2
Write the regular expression that will find all lines that end with uppercase T. How many lines end with uppercase T?
regex.018.Word Boundaries
Write the regular expression that finds all the sequences of lowercase letters that start with ins. How many lines contain sequences that match the regular expression?
regex.019.Alternation - Question 1 (True/False)
Concatenation has higher precedence that alternation.
regex.020.Alternation - Question 2
Write the regular expression that only matches the words make or time. Only match complete words. How many distinct matches are there for the words make and time in the file?
regex.021.Grouping and Backreferences - Question 1
Write a regular expression that uses backreferences to match complete words that contain two occurrences of a repeated letter (like occurrences and massachusetts). How many UNIQUE words that match the pattern are in the file?
regex.022.Grouping and Backreferences - Question 2
Write a regular expression that uses backreferences to match complete words of lower case letters that start and end with the same two letters, but the letters are reversed at the end of the word (like deflated, toot and series). How many UNIQUE words that match the pattern are in the file?