library(stringr)

QUESTION 3 - Copy the introductory example. The vector name stores the extracted names.

raw.data <-"555-1239Moe Szyslak(636) 555-0113Burns, C. Montgomery555-6542Rev. Timothy Lovejoy555 8904Ned Flanders636-555-3226Simpson, Homer5553642Dr. Julius Hibbert"
name <- unlist(str_extract_all(raw.data, "[[:alpha:]., ]{2,}"))
name
## [1] "Moe Szyslak"          "Burns, C. Montgomery" "Rev. Timothy Lovejoy"
## [4] "Ned Flanders"         "Simpson, Homer"       "Dr. Julius Hibbert"
  1. Use the tools of this chapter to rearrange the vector so that all elements conform to the standard; first_name last_name
firstName <- str_extract_all(unlist(str_extract_all(name,"[:alpha:]{1,25} |, [:print:]{1,25}")),"[A-Z](.+?)+[a-z]")
firstName
## [[1]]
## [1] "Moe"
## 
## [[2]]
## [1] "C. Montgomery"
## 
## [[3]]
## [1] "Timothy"
## 
## [[4]]
## [1] "Ned"
## 
## [[5]]
## [1] "Homer"
## 
## [[6]]
## [1] "Julius"
lastName <- str_extract_all(unlist(str_extract_all(name,"[a-z] [:alpha:]{1,25}[a-z]$|[:print:]{1,25},")),"[A-z][a-z]+|[A-z][a-z]+,")
lastName
## [[1]]
## [1] "Szyslak"
## 
## [[2]]
## [1] "Burns"
## 
## [[3]]
## [1] "Lovejoy"
## 
## [[4]]
## [1] "Flanders"
## 
## [[5]]
## [1] "Simpson"
## 
## [[6]]
## [1] "Hibbert"
  1. Construct a logical vectore indicating whether a character has a title
titlechecker <- str_detect(firstName, "[[:alpha:]]{2,}\\.")
titlechecker
## [1] FALSE FALSE FALSE FALSE FALSE FALSE
  1. Construct a logical vectore indicating whether a character has a second name
lastnamechecker <- str_detect(lastName, "[[:alpha:]]+$")
lastnamechecker
## [1] TRUE TRUE TRUE TRUE TRUE TRUE

QUESTION 4 - Describe the types of strings that conform to the following regular expressions and construct an example that is matched by the regular expression.

  1. [0-9]+\$ a string of one or more digits followed by a dollar sign

  2. \b[a-z]{1,4}\b a word of up to four lowercase alphabetic characters

  3. .*?\.txt$ string of characters ending in “.txt”

  4. \d{2}/\d{2}/\d{4} date format of XX/XX/XXXX

  5. <(.+?)>.+?</\1> string of characters with start and end tags

QUESTION 9 - The following code hides secret message. crack it with R and regular expressions.

secret <- "clcopCow1zmstc0d87wnkig7OvdicpNuggvhryn92Gjuwczi8hqrfpRxs5Aj5dwpn0TanwoUwisdij7Lj8kpf03AT5Idr3coc0bt7yczjatOaootj55t3Nj3ne6c4Sfek.r1w1YwwojigOd6vrfUrbz2.2bkAnbhzgv4R9i05zEcrop.wAgnb.SqoU65fPa1otfb7wEm24k6t3sR9zqe5fy89n6Nd5t9kc4fE905gmc4Rgxo5nhDk!gr"

unlist(str_extract_all(secret, "[A-Z]"))
##  [1] "C" "O" "N" "G" "R" "A" "T" "U" "L" "A" "T" "I" "O" "N" "S" "Y" "O"
## [18] "U" "A" "R" "E" "A" "S" "U" "P" "E" "R" "N" "E" "R" "D"

CONGRATULATIONS YOU ARE A SUPERNERD