Perl Regular Expression - Quiz Explanation

The answers you selected are indicated below, along with text that explains the correct answers.

 
1. Which metacharacter matches the end of a string?
Please select the best answer.
  A. $
  B. ^
  C. |
  The correct answer is A. The $ metacharacter matches the end of a string, while the ^ metacharacter matches the beginning of a string. The pipe character ( | ) separates alternate matches.

2. What does this expression match?
/^[abcd]/

Please select the best answer.
  A. Any string with an a, b, c, or d in it
  B. Any string that begins with a, b, c, or d
  C. Any string that begins with a lowercase a, b, c, or d
  The correct answer is C. It will match any string that begins with any of the the lowercase letters that are specified in the expression.

3. What does this expression do?
s/\b(\w+)\s+(\1)\b/$1/ig;
Please select the best answer.
  A. Doubles every word (for example, now now is is the the) in a line
  B. Removes doubled words (for example, now is the the time for...) from a line
  C. Removes all the w's from a line
  The correct answer is B. This expression removes doubled words from a line of text.

4. What does this line do?
while(<>) { /^(from|subject):/i and print }
Please select the best answer.
  A. Removes the headers from your email and prints out the body
  B. Removes the headers from your email and prints a blank line
  C. Prints out all the From: and Subject: lines from a mailbox
  The correct answer is C. This is very similar to what I use to check my mailbox periodically. It checks for a From: or Subject: header and prints the line when it finds one.

5. What does this loop do?
while(<>) {
  s/^(Subject:)\s*(.*)$/$1 Re: $2/i unless /re:/i;
  print;
  }

Please select the best answer.
  A. Adds Re: to an email subject, if it's not already there
  B. Removes the subject from email
  C. Makes the Re: into lowercase
  The correct answer is A. This loop isolates the subject, and adds Re: to it before printing it. It leaves alone any lines that already have Re: in them.

6. Which one of the following regular expressions would match the word in and the word inch, but not the word grin?
Please select the best answer.
  A. /\bin\b/
  B. /\bin/
  C. in\b/
  The correct answer is B. Choice A would only match the word in, whereas Choice C would match any other word that ended with the pattern in, which would include the word grin but not the word inch.

7. What does this loop do:
while(<>) {
  s/^(.*Foo.*)$/print($1)/ie;
}

Please select the best answer.
  A. Deletes all the lines with Foo in them
  B. Prints all the lines with Foo in them
  C. Deletes all the lines without Foo in them
  The correct answer is B. The e modifier tells perl to execute the replacement string, instead of just replacing with it.

8. Which of the following regular expressions will match this string:
AaZzXzY
Please select the best answer.
  A. /x/
  B. /^[a-z]*$/
  C. /^\w*$/
  The correct answer is C. There is no lowercase x in the string, and there are some characters other than the lowercase a-z. On the other hand, all the characters in the string are in the \w class.