Example as Shown below:
library (stringr)
## Warning: package 'stringr' was built under R version 3.2.2
str_extract("1234$ is a asdasdas dsadas$ $12345 3456$ 57648", "[0-9]+\\$")
## [1] "1234$"
str_extract_all("1234$ is a asdasdas dsadas$ $12345 3456$ 57648", "[0-9]+\\$")
## [[1]]
## [1] "1234$" "3456$"
b)\b[a-z]{1,4}\b Type of String: Anchor Sequence in R to match a word boundary \b and matches with Regex character classes of any lower case ASCII letter [a-z] with Quantifier {1,4} which means precending items will match atleast 1 time but not more than 4 times
expb="123 is the way to DO anything unIque AND make amazing things"
str_extract(expb,"\\b[a-z]{1,4}\\b")
## [1] "is"
str_extract_all(expb,"\\b[a-z]{1,4}\\b")
## [[1]]
## [1] "is" "the" "way" "to" "make"
c).?\.txt$ Type of String: Quantifier means preceding item will be matched zero or more times and ? means preceding items is optional and will be matched atmost once. So any character ahead of . metacharacter as .txt will be considered
str_extract("adas asdasd adasd _txt", ".*?\\.txt$")
## [1] NA
str_extract("Iamhere.txt",".*?\\.txt$")
## [1] "Iamhere.txt"
str_extract("12\12\2012","\\d{2}/\\d{2}/\\d{4}")
## [1] NA
str_extract("12/12/2012","\\d{2}/\\d{2}/\\d{4}")
## [1] "12/12/2012"
e)<(.+?)>.+?
str_extract("<font> </head>","<(.+?)>.+?</\\1>")
## [1] NA
str_extract("<font>I am an awesome font</font>","<(.+?)>.+?</\\1>")
## [1] "<font>I am an awesome font</font>"
str_extract("<HTml>AM I correct </html>","<(.+?)>.+?</\\1>")
## [1] NA
str_extract("1234$ is a asdasdas dsadas$ $12345 3456$ 57648", "\\d{1,}[$]")
## [1] "1234$"
str_extract_all("1234$ is a asdasdas dsadas$ $12345 3456$ 57648", "\\d{1,}[$]")
## [[1]]
## [1] "1234$" "3456$"
emailID="chunkylover53[at]aol[dot]com"
emailID=sub("\\[at]","@",emailID)
emailID=sub("\\[dot]",".",emailID)
emailID
## [1] "chunkylover53@aol.com"
str_extract_all("chunkylover53[at]aol[dot]com","[[:digit:]]{1,}")
## [[1]]
## [1] "53"
c.Instead of using the predefined character classes, we would like to use the predefined symbols to extract the digits in the mail address. To do so we write the expression \D. Explain why this fails and correct the expression. Answer: Failed because of Wrong syntax. \D is for non digit characters and for digit charaters we use \d
str_extract_all("chunkylover53[at]aol[dot]com","\\d{1,}")
## [[1]]
## [1] "53"