Workshop

If you've started to figure out the pattern to regular expressions, try this quiz to see what you've learned.

Quiz

1:If you have lines formatted "x=y", what expression would swap the left and right sides of the expression?
  1. s/(.+)=(.+)/$2=$1/;

  2. s/(*)=(*)/$2=$1/;

  3. s/(.*)=(.*)/$2$1/;

2:After this code, what's the value in $2?
$foo="Star Wars: The Phantom Menace";
$foo=~/star\s((Wars): The Phantom Menace)/;
  1. $2 is not set after the pattern match because the match fails.

  2. Wars

  3. Wars: The Phantom Menace

3:What does the pattern m/^[-+]?[0-9]+(\.[0-9]*)?$/ match?
  1. Dates in the format 04-03-1969

  2. Well-formed numbers such as 45, 15.3, -0.61

  3. Addition-looking patterns: 4+12 or 89+2

Answers

A1: a. Choice c doesn't include the = symbol in the replacement string ...

Get SAMS Teach Yourself Perl in 24 Hours THIRD EDITION now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.