Exercise : Regular Expressions (by Rishi Goutam)

Take words.txt and regex.php and print out the words that match the following conditions:

  1. Both the Martys
  2. The Stepp brothers
  3. The hw directories but not the mistyped "hwFour"
  4. The words "Babbage" through the words "scabbards"
  5. Any variation of "yes"
  6. Words of length seven
  7. Characters from the Dilbert comic
  8. Something of your choice

Exercise Solution

<?php
   // This solution puts all regular expressions into an array
   // and selects one at a time.
   $regexps = array(
      "/Marty/",     // 1. Both Martys
      "/Stepp/",     // 2. The Stepp Brothers
      "/hw\d/",      // 3. HW Directories
      "/abba/",      // 4. Babbage through scabbards
      "/^(yes)$/i",  // 5. Any variation of "yes"
      "/^\w{7}$/",   // 6. Words of length seven
      "/bert$/"      // 7. Characters from Dilbert
   );
   $regexp = $regexps[6];
?>