1. Copy the introductory example. The verctor name stores the extracted names. (a) use tools in this chapter to rearrange the vector so that all elements conforms to the standard firstname_lastname. (b)Construct a logical vector indicating whether a charater has title or not(i.e.,Rev.,Dr.) (c)Construct a logical vector indicating whether a character has a second name.
# Answer to question (a)
library(stringr)
name<-c("Moe Szyslak","Burns, C. Montgomery","Rev. Timothy Lovejoy","Ned Flanders","Simpson, Homer", "Dr. Julius Hibbert") 
std_name<-str_replace_all(name, c(","),"")
std_name<-str_replace_all(std_name, c("Rev. "),"")
std_name<-str_replace_all(std_name, c("Dr. "),"")
std_name<-str_replace_all(std_name, c("C. "),"")
std_name<-str_replace_all(std_name, c(" "),"_")
std_name
## [1] "Moe_Szyslak"      "Burns_Montgomery" "Timothy_Lovejoy" 
## [4] "Ned_Flanders"     "Simpson_Homer"    "Julius_Hibbert"
# Answer to question (b)

title_present<-str_detect(name,c("Rev.","Dr."))
title_present
## [1] FALSE FALSE  TRUE FALSE FALSE  TRUE
# Answer to question (c)

name1<-str_replace_all(name, c(","),"")
name1<-str_replace_all(name1, c("Rev. "),"")
name1<-str_replace_all(name1, c("Dr. "),"")
secondname_present<-str_detect(name1,"[[:alpha:]+.]{1,} [[:alpha:]+.]{1,} [[:alpha:]+.]{1,}") 
secondname_present
## [1] FALSE  TRUE FALSE FALSE FALSE FALSE
  1. Consider the HTML teag “ +++BREAKING NEWS+++ " we would like to extract the first HTML tag. to do so we write the regular expression <.+>. Explain why this fails and correct the expression.
HTMLtag1<-("<title>+++BREAKING NEWS+++</title>")
# with the expression <.+> , code look into all the values including first > , with (.) expression. thats why expression returns all the characters within that strings. Correct expression is given below. 
str_extract(HTMLtag1, "<[:alpha:]+>")
## [1] "<title>"
  1. Consider the string “(5-3)$2=5$2-253+3$2”, conforms to bionomial theorem. We would like to extact the formula and in the string. in order to do so we write the regular expression [$0-9-=+*()]+. Explain why this fails and correct this expression.
expression<-c("(5-3)$2=5$2-2*5*3+3$2") 
# expression [$0-9-=+*()]+  digit 0-9 is not included within the braces and does not return the values
str_extract(expression,"[$[0-9]-=+*()]+")
## [1] "(5-3)$2=5$2-2*5*3+3$2"
  1. Following code hides a secret message.Crack it with R and regular expressions. “clcopCow1zmstc0d87wnkig7OvdicpNuggvhryn92Gjuwczi8hqrfpRxs5Aj5dwpn0TanwoUwisdij7L j8kpf03AT5Idr3coc0bt7yczjatOaootj55t3Nj3ne6c4Sfek.r1w1YwwojigOd6vrfUrbz2.2bkAnbhz gv4R9i05zEcrop.wAgnb.SqoU65fPa1otfb7wEm24k6t3sR9zqe5fy89n6Nd5t9kc4fE905gmc4Rgxo5nhDk!gr”
x<-c("clcopCow1zmstc0d87wnkig7OvdicpNuggvhryn92Gjuwczi8hqrfpRxs5Aj5dwpn0TanwoUwisdij7Lj8kpf03AT5Idr3coc0bt7yczjatOaootj55t3Nj3ne6c4Sfek.r1w1YwwojigOd6vrfUrbz2.2bkAnbhzgv4R9i05zEcrop.wAgnb.SqoU65fPa1otfb7wEm24k6t3sR9zqe5fy89n6Nd5t9kc4fE905gmc4Rgxo5nhDk!gr")
  
x1<-unlist(str_extract_all(x, "[[:upper:].]{1,}"))
paste(x1, collapse = '')
## [1] "CONGRATULATIONS.YOU.ARE.A.SUPERNERD"