CS 1206: Assignment 3

Due Friday, February 26, 1999 by 5pm

100 Points




1. Regular Expressions: What Strings Match an RE?

Consider the following text, taken from the manual page for a command called "flip" (if you would like to use flip, you can find it compiled for FreeBSD along with it's man page in the directory /home/faculty/sedwards/bin on the lab machines):

Flip is a file interchange program that converts text file
formats between **IX and MS-DOS.  It converts lines ending
with carriage-return (CR) and linefeed (LF) to lines ending
with just linefeed, or vice versa.
If we refer to these lines by number (1 through 4), then which line(s), if any, match each of the following (grep) REs?
  1. /feed/
  2. /file$/
  3. /^[Ff]/
  4. /.$/
  5. / */
  6. /lines*/
  7. /lines\{0,\}/
  8. /[Cc].*[Ff]/
  9. /([Cc].*[D-G])/
  10. /[Ii][at][a-z]*/
Here, a line matches if there is at least one substring in the line that matches the given RE. Also, in the REs above "(" and ")" characters used alone match themselves, while "\(" and "\)" are used for grouping (these are the rules that grep and ex/vi use, while egrep uses the opposite conventions). All of these REs are also case-sensitive.

If you are unsure about a question, feel free to try out a few experiments with grep or with the "search" command / in vi. Remember that neither of these programs expect you to enter the "/" delimiters when you type the RE for which to search, and that both use case sensitive searches by default.

2. Regular Expressions: What Strings Match an RE? (cont.)

For the next set of (egrep) REs, identify all substrings that match each RE (without crossing lines, of course) in the four lines listed in Section 1. You can do this by listing all the matched strings. Remember that REs match the longest possible string. For example, the egrep RE /\(.*\)/ matches the following string(s):

(CR) and linefeed (LF)
Find all possible matches for each of the following (egrep) REs:
  1. /F[^ ]*/
  2. /line(s|[^s ]+)/
  3. /c.*s/
  4. /[a-z]*[e.]$/
  5. /\*+/

3. Regular Expressions: Writing REs

Now it is your turn to write some REs to match specific text fragments. For each of the following questions, write an RE that matches the specified text (including but not limited to all of the underlined phrases in each example) and no other text in the given line, assuming that your RE is intended to be used with grep.

  1. Matching "hello", "hi", or "howdy":
    hello, there.  or is "hi" or "howdy" more to your liking?
    -----                 --      -----
    
  2. Matching "the", regardless of case:
    The quick brown fox jumps over the lazy dog.
    ---                            ---
    
  3. The last word in a sentence:
    How many sentences are here? There are two.  No, three!
                           -----           ----      ------
    
  4. A social security number:
    The number 045-35-2344 is a random SSN.
               -----------
    
  5. A word with five or more letters:
    This sentence does not have many long words.
         --------                         -----
    
  6. Any word beginning with the root "reus":
    reusable software is software that was designed to be reused.
    --------                                              ------
    
  7. An entire sentence that ends in a period:
    Does this sentence end in a period?  This one does.
                                         --------------
    
  8. Any sequence beginning with "artificial" and ending with "intelligence":
    Politicians can act artificial, but do they have intelligence?
                        -----------------------------------------
    
  9. Any of "computer", "computers", or "computing":
    computer science is the study of computing, and how computers work.
    --------                         ---------          ---------
    
  10. Matching any phrase of exactly three words separated by white space:
    This is a short sentence.
    ---------
         ----------
            ----------------
    
For these questions, you may not simply list an underlined phrase itself--you must use at least one special character in your answer.

Part III: Submitting Your Answers

Your submission must follow exactly the format described below, with no extra lines, omitted questions, or extraneous comments. For questions 1 through 10, give the problem number, a period, a space, and then the line numbers in order of the RE matches, separated by commas. For instance

1. 1,3,4
2. 2,3
3. 
4. 2
etc.
Give one problem per line, with no intervening extraneous lines. If there are no matches, terminate the line after the space following the period.

For questions 11 through 15, enter the problem number followed by a period on a line with nothing else. Then list the matched strings one per line, with no leading or trailing blanks, in the order they occur, left to right, top line to bottom line. Follow all the lines containing matched strings with exactly one blank line. For instance, the RE /f.*m/ would be answered with

11.
file interchange program
form

12.
etc.

For questions 16 through 25, enter the problem number followed by a period, a space, a slash, the grep RE, and a slash terminating the line. For instance,

16. /h[eio][a-z]*/
17. /t[a-z]*/
etc.

You are to hand in your assignment by using the Automated Acceptor application. To receive credit, your assignment must be submitted before the time and date listed above. It is your responsibility to successfully submit your assignment via the Automated Acceptor. A maximum of three submissions is allowed per student. Brief instructions are given below on how to use the Acceptor, but it will be helpful to visit the Automated Acceptor's web page.

After connecting to the Internet, run the acceptor program with the command:

Acceptor_UI

A dialog box will then appear. Fill in your original PID, e-mail password, and Student ID. Click inside the Course Index field and select the lab section you are enrolled in. Type in HW3 for the Project Number. Enter the full path of the file to be submitted, or click on Browse to find it. After all the information is typed in, click on Submit. If your submission was successful, a window will appear stating "Your file has been submitted. A confirmation will be sent to you via e-mail." If your submission did not succeed, then a dialog box will appear describing the problem. Correct the information and resubmit. If the submission was successful, click the Quit button. After a few minutes (or sometimes hours!), you should receive an e-mail message verifying the file was received.

Also, remember that no late assignments are accepted.




L. T. Watson <ltw@cs.vt.edu>
Last modified: Tue Feb 2 15:46:35 EST 1999