CS 2304--UNIX: Assignment 3

Due Friday, February 28, 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 ~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 emacs 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" menu in emacs. Remember that neither of these programs expect you to enter the "/" delimiters when you type the RE for which to search. Also, note that emacs normally ignores case in 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 underlining all matches. For example, the RE /(.*)/ matches the following:

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

  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 cs2304u@ei.cs.vt.edu. To receive credit, you 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.

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




Stephen Edwards <edwards@cs.vt.edu>
Last modified: Wed Feb 19 10:10:26 EST 1997