Puzzle Activity: Regular Expressions


Run the code below to load the scrambled message:

message <- read.csv("https://www.dropbox.com/s/lgpn3vmksk3ssdo/scrambled_message.txt?dl=1", stringsAsFactors = FALSE)$Word

In this activity, a “word” is a set of characters with no white space. That is, even though many of elements of the scrambled mess vector are nonsense, and some have punctuation, you can consider each element to be a “word”.

Warm-up exercises

  1. How many characters are in the scrambled message?
  2. How many of these characters are white space?
  3. How many words are in the scrambled message?
  4. Show all the punctuation marks in the scrambled message.
  5. Print out, in all capitals, the longest word in the scrambled message.
  6. Print out every piece of a word that starts with the letter “m” and ends with the letter “z” in the scrambled message.

Decode a message

Complete the following steps to decode the message.

  1. Remove any spaces before or after each word.
  2. No word should be longer than 16 characters. Drop all extra characters off the end of each word.
  3. Any time you see the word “ugh”, with any number of h’s, followed by a punctuation mark, delete this.
  4. Replace all instances of exactly 2 a’s with exactly 2 e’s.
  5. Replace all z’s with t’s.
  6. Every word that ends in b, change that to a y. Hint: look out for punctuation!
  7. Every word that starts with k, change that to a v. Hint: look out for capitalization!
  8. Use str_c to recombine all your words into a message.
  9. Find the movie this quote is fromn.