library(stringr)
library(XML)
library(RCurl)
## Loading required package: bitops
Split names by comma, flip, combine.
name<- c(
"Moe Szyslak",
"Burns, C. Montgomery",
"Rev. Timothy Lovejoy",
"Ned Flanders",
"Simpson, Homer",
"Dr. Julius Hibbert")
hascomma<-str_detect(name,",")
hascomma
## [1] FALSE TRUE FALSE FALSE TRUE FALSE
splitname<-str_split(name,",")
splitname
## [[1]]
## [1] "Moe Szyslak"
##
## [[2]]
## [1] "Burns" " C. Montgomery"
##
## [[3]]
## [1] "Rev. Timothy Lovejoy"
##
## [[4]]
## [1] "Ned Flanders"
##
## [[5]]
## [1] "Simpson" " Homer"
##
## [[6]]
## [1] "Dr. Julius Hibbert"
flipname<-sapply(splitname,rev)
flipname
## [[1]]
## [1] "Moe Szyslak"
##
## [[2]]
## [1] " C. Montgomery" "Burns"
##
## [[3]]
## [1] "Rev. Timothy Lovejoy"
##
## [[4]]
## [1] "Ned Flanders"
##
## [[5]]
## [1] " Homer" "Simpson"
##
## [[6]]
## [1] "Dr. Julius Hibbert"
goodname<-str_c(flipname,sep=" ")
goodname
## [1] "Moe Szyslak" "c(\" C. Montgomery\", \"Burns\")"
## [3] "Rev. Timothy Lovejoy" "Ned Flanders"
## [5] "c(\" Homer\", \"Simpson\")" "Dr. Julius Hibbert"
# Needs work.
Look for 2 or more letters followed by a period.
hastitle<-str_detect(name,"[[:alpha:]]{2,}\\.")
hastitle
## [1] FALSE FALSE TRUE FALSE FALSE TRUE
Look for 1 uppercase letter followed by a period.
multiplenames<-str_detect(name,"[[:upper:]]{1}\\.")
multiplenames
## [1] FALSE TRUE FALSE FALSE FALSE FALSE
# This would not detect first names without a period.
The original expression failed because it was extracting the longest matching result. To get the desired result, we can extract the tag with only letter (alpha) characters.
astring<-c("<title>++BREAKING NEWS+++</title>")
firsttag<-str_extract(astring,"<[[:alpha:]]+>")
firsttag
## [1] "<title>"
The original expression failed because it did not capture the carat or minus sign.
bstring<-c("(5-3)^2=5^2-2*5*3+3^2 conforms to the binomial theorem")
bformula<-str_extract(bstring,"[0-9=+*()+\\^\\-]+")
bformula
## [1] "(5-3)^2=5^2-2*5*3+3^2"
secretmessage <- c("clcopCow1zmstc0d87wnkig7OvdicpNuggvhryn92Gjuwczi8hqrfpRxs5Aj5dwpn0TanwoUwisdij7Lj8kpf03AT5Idr3coc0bt7yczjatOaootj55t3Nj3ne6c4Sfek.r1w1YwwojigOd6vrfUrbz2.2bkAnbhzgv4R9i05zEcrop.wAgnb.SqoU65fPa1otfb7wEm24k6t3sR9zqe5fy89n6Nd5t9kc4fE905gmc4Rgxo5nhDk!gr")
decode<-unlist(str_extract_all(secretmessage,"[[:upper:]]|\\."))
combined<-str_c(decode,sep="",collapse ="")
answer<-gsub("\\."," ",combined)
cat(answer)
## CONGRATULATIONS YOU ARE A SUPERNERD
justforme<-(str_c(answer, " ARMENOUSH"))
cat(justforme)
## CONGRATULATIONS YOU ARE A SUPERNERD ARMENOUSH