Amazing technological breakthrough possible @S-Logix pro@slogix.in

Office Address

  • #5, First Floor, 4th Street Dr. Subbarayan Nagar Kodambakkam, Chennai-600 024 Landmark : Samiyar Madam
  • pro@slogix.in
  • +91- 81240 01111

Social List

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

  Load the data file

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

Screenshots
extract pattern using regular expression in R with stringR package
 Extract all matches
Extract the first match