DH

library(stringr)

#3
##names
simpsons <- c("Moe Syzlak", "Burns, C. Montgomery", "Rev. Timothy Lovejoy", "Ned Flanders", "Simpson, Homer", "Dr. Julius Hibbert")

##identify when last name is first
lastname <- str_detect(simpsons,",")
lastname
## [1] FALSE  TRUE FALSE FALSE  TRUE FALSE
##reorder names and remove the white spaces between names
sub("([^,]+),\\s*([^,]+)", "\\2 \\1", simpsons)
## [1] "Moe Syzlak"           "C. Montgomery Burns"  "Rev. Timothy Lovejoy"
## [4] "Ned Flanders"         "Homer Simpson"        "Dr. Julius Hibbert"
##identify names preceded by titles
title <- str_detect(simpsons, "Rev.|Dr.")
title
## [1] FALSE FALSE  TRUE FALSE FALSE  TRUE
##identify second names
second_names <- str_count(simpsons, " ") > ifelse(title, 2, 1)
second_names
## [1] FALSE  TRUE FALSE FALSE FALSE FALSE
#7
##original string will identify all the characters
str_extract("<title>+++BREAKING NEWS+++</title>", "<.+>")
## [1] "<title>+++BREAKING NEWS+++</title>"
##identifying the first html tag requires a ?
str_extract("<title>+++BREAKING NEWS+++</title>", "<.+?>")
## [1] "<title>"
#8
##original string returns a dash
str_extract("(5-3)^2=5^2-2*5*3+3^2 conforms to the binomial theorem", "[^0-9=+*()]+")
## [1] "-"
##the original string did not work because the ^ and - need to be identified as such by \\
str_extract("(5-3)^2=5^2-2*5*3+3^2 conforms to the binomial theorem", "[\\^\\-0-9=+*()]+")
## [1] "(5-3)^2=5^2-2*5*3+3^2"
#9
code <- c("clcopCow1zmstc0d87wnkig7OvdicpNuggvhryn92Gjuwczi8hqrfpRxs5Aj5dwpn0TanwoUwisdij7Lj8kpf03AT5Idr3coc0bt7yczjatOaootj55t3Nj3ne6c4Sfek.r1w1YwwojigOd6vrfUrbz2.2bkAnbhzgv4R9i05zEcrop.wAgnb.SqoU65fPa1otfb7wEm24k6t3sR9zqe5fy89n6Nd5t9kc4fE905gmc4Rgxo5nhDk!gr")

##tried using regmatches but all of the non upper case letters returned "" and was not sure how to remove those
regmatches(code, gregexpr('[[:punct:]]*[[:upper:][:punct:]]*', code))
## [[1]]
##   [1] ""   ""   ""   ""   ""   "C"  ""   ""   ""   ""   ""   ""   ""   ""  
##  [15] ""   ""   ""   ""   ""   ""   ""   ""   ""   ""   "O"  ""   ""   ""  
##  [29] ""   ""   "N"  ""   ""   ""   ""   ""   ""   ""   ""   ""   ""   "G" 
##  [43] ""   ""   ""   ""   ""   ""   ""   ""   ""   ""   ""   ""   "R"  ""  
##  [57] ""   ""   "A"  ""   ""   ""   ""   ""   ""   ""   "T"  ""   ""   ""  
##  [71] ""   "U"  ""   ""   ""   ""   ""   ""   ""   "L"  ""   ""   ""   ""  
##  [85] ""   ""   ""   "AT" ""   "I"  ""   ""   ""   ""   ""   ""   ""   ""  
##  [99] ""   ""   ""   ""   ""   ""   ""   ""   "O"  ""   ""   ""   ""   ""  
## [113] ""   ""   ""   ""   "N"  ""   ""   ""   ""   ""   ""   ""   "S"  ""  
## [127] ""   ""   "."  ""   ""   ""   ""   "Y"  ""   ""   ""   ""   ""   ""  
## [141] "O"  ""   ""   ""   ""   ""   "U"  ""   ""   ""   ""   "."  ""   ""  
## [155] ""   "A"  ""   ""   ""   ""   ""   ""   ""   "R"  ""   ""   ""   ""  
## [169] ""   "E"  ""   ""   ""   ""   "."  ""   "A"  ""   ""   ""   ".S" ""  
## [183] ""   "U"  ""   ""   ""   "P"  ""   ""   ""   ""   ""   ""   ""   ""  
## [197] "E"  ""   ""   ""   ""   ""   ""   ""   ""   "R"  ""   ""   ""   ""  
## [211] ""   ""   ""   ""   ""   ""   ""   "N"  ""   ""   ""   ""   ""   ""  
## [225] ""   ""   "E"  ""   ""   ""   ""   ""   ""   ""   "R"  ""   ""   ""  
## [239] ""   ""   ""   "D"  ""   "!"  ""   ""
##used gsub to unlock the message
gsub("[^A-Z]", "", code)
## [1] "CONGRATULATIONSYOUAREASUPERNERD"