• 2nd Floor, #7a, High School Road, Secretariat Colony Ambattur, Chennai-600053 (Landmark: SRM School) Tamil Nadu, India
• pro@slogix.in
• +91- 81240 01111

### How to extract pattern using regular expression in R with stringR package?

###### Description

To extract pattern using regular expression in R with stringR package

#### Functions Used

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

###### Process

Convert the data required as a character vector

Use the predefined function to extract pattern

###### Sapmle Code

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”)