Previous Table of Contents Next


Quiz 5

1.  Suppose you’re solving a crossword puzzle and you need a seven-letter word with t as the third letter, r as the sixth, and n as the seventh. You have a UNIX system, which probably keeps a list of words (one per line) in the file /usr/dict/words. What pattern would you use to find all the matching words?
a.  
/^..t..rn/i
b.  
/..t..(rn)$/i
c.  
/^..t..rn$/i
d.  
/\A\W\Wt\W\Wrn\Z/i
2.  Which statement could you use to find the name of the actress in the string
$drivers = "Director: Deniro \nActress: Shepherd \nActor: Scorsese";
a.  
($actress) = ($drivers =~ /\AActress: (\w+).?\Z/m);
b.  
$actress = ($drivers =~ /^Actress: (\w+).?$/m);
c.  
($actress) = ($drivers =~ /^Actress: (\w+).?$/m);
d.  
($actress) = ($drivers =~ /\AActress: (\w+).?\Z/s);
3.  What’s printed by the following code?
$movie = "New: Bambi meets Godzilla...in 3-D!";
$movie =~ /(\w+) meets (\w+)/;
print "$'$`$&$+";
a.  
New:...in 3-D! Bambi meets GodzillaGodzilla
b.  
...in 3-D!New: Bambi meets Godzilla
c.  
...in 3-D!New: New: Bambi meets Godzilla...in 3-D!Godzilla
d.  
...in 3-D!New: Bambi meets GodzillaGodzilla
4.  Here’s hypothetical output from the MS-DOS dir command:
MOVIE TXT 8192 2-13-97 2:55am
MOVIES <DIR> 2-12-97 11:58pm

MOVIE.TXT is a file; MOVIES is a subdirectory. You can tell because MOVIES.TXT has a numeric size, 8192, while MOVIES has the string <DIR> instead.

Suppose you want to use this distinction to list all the subdirectories in a given directory, making use of the fact that the size, if present, must appear before the date (represented by \d+-\d+ ). Given the following two lines

($name, $size) = /$regexp/;
print "$name is a directory" unless $size;

what could $regexp be?

a.  
/^\w+.*\d+.*\d+-\d+/
b.  
/^(\w+).*(\d+).*\d+-\d+$/
c.  
/^(\w+)<DIR>(\d+).*\d+-\d+/
d.  
/^(\w+).*(\d*).*\d+-\d+/

Exercise 5

Difficulty: Hard

Rewrite advent to accept an optional a, an, or the before the noun and use it in the messages it prints, so that

go to the school

yields

Where is the school?

Also, let the user enter three commands at a time.

Hint: Use ? to handle optional words. Use the concatenation operator (.) to create a single string with embedded newlines. And use /s or /m to separate the user’s input into the three commands.

Session 6
Advanced regexes I and grep()

The last three sessions of this chapter cover a wide assortment of regex features. The most frequently used features are covered first, in this session.

Control Characters

You’ve seen that you can include \n in both print() statement and regular expressions. That’s also true of \r (carriage return, which means “move the cursor to the left edge of the page/screen”, in contrast to \n, which either means “move down one line” or “move to the left edge and down one line” depending on what sort of computer you’re using). In fact, most any ASCII control character can be used in both double-quoted strings and regexes. Table 2-2 shows some of the commonly used control characters. A notable exception from this list is \b, which is a backspace when printed, but a word boundary inside a regex.

Table 2-2 Some control characters

control character meaning

\n newline
\t tab
\a alarm (the system beep)
\r return
\f formfeed
\cD [CTRL]-[D]
\cC [CTRL]-[C]


Previous Table of Contents Next