To extract pattern using regular expression in R with stringR package
str_extract(string,pattern) – extract first match
str_extract_all(string,pattern) – extract all matches
str_extract_all(string,patter,simplify) – simplify results into character matrix
str_extract_all(string,pattern,boundary) – Extract all words
str_replace(string,pattern,replacement) – To replace first match
str_replace_all(string,pattern,replacement) – To replace all matches
Load the data file
Convert the data required as a character vector
Use the predefined function to extract pattern
shopping_list #Extract the first match
str_extract(shopping_list, “\\d”)
str_extract(shopping_list, “[a-z]+”)
str_extract(shopping_list, “[a-z]{1,4}”)
str_extract(shopping_list, “\\b[a-z]{1,4}\\b”)
# Extract all matches
str_extract_all(shopping_list, “[a-z]+”)
str_extract_all(shopping_list, “\\b[a-z]+\\b”)
str_extract_all(shopping_list, “\\d”)
# Simplify results into character matrix
str_extract_all(shopping_list, “\\b[a-z]+\\b”, simplify = TRUE)
str_extract_all(shopping_list, “\\d”, simplify = TRUE)
# Extract all words
str_extract_all(“This is, suprisingly, a sentence.”, boundary(“word”))
#To replace first match
str_replace(c(“have”,”Fast”,”drive”,”go”,”have to serve”),”ve”,”ving”)
#To replace all matches
str_replace_all(c(“ever and ever”,”server”,”faster than ever”,”have to serve”),”er”,”s”)