CS 1206: Assignment 3

Due Friday, February 20, 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 REs?
  1. /file/
  2. /^f/
  3. /^[Ff]/
  4. /./
  5. / */
  6. /line\(s\)?/
  7. /convert|change/
  8. /[Cc].*[Rr]/
  9. /text.*file/
  10. /[a-z][a-z]*ing/
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 ex/vi uses, 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 egrep 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 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 RE /(.*)/ matches the following string(s):

(CR) and linefeed (LF)
Find all possible matches for each of the following (egrep) REs:
  1. /[Ff][^ ]*/
  2. /line[a-z]*/
  3. /c.*r/
  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.

Submitting Your Answers

You are to hand in your assignment by sending an email message to the address cs1206@ei.cs.vt.edu. To receive credit, your mail message must be received by this account by the time and date listed above. It is your responsibility to successfully submit your assignment via email (remember that you will receive an auto-reply by the system when your message has been received successfully). Also, send yourself a copy for future reference. The messsage must be a plain ASCII text file, with no attachments, and in exactly the format defined below. Do not insert any extra notes or explanations.

The body of your message must be a plain ASCII text file that contains the following:




L. T. Watson <ltw@cs.vt.edu>
Last modified: Thu Feb 19 12:01:08 EST 1998