#5, First Floor, 4th Street , Dr. Subbarayan Nagar Kodambakkam, Chennai-600 024 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 locate,extract and replace pattern using regular expression in R?
Description

To locate,extract and replace pattern using regular expression in R

Functions Used

regexpr(pattern,x) – To find starting position and length of first match
gregexpr(pattern,x) – To find starting position and length of all matches
regmatches(x,regexpr(pattern,x) or gregexpr(pattern,x)) – To Extract first match or extract all match
sub(pattern,replacement,x) – To replace first match
gsub(pattern,replacement,x) – To replace all matches

Process

  Convert the data required as a character vector

  Use the predefined function regexpr(),gregexpr() to locate the pattern,regmatches() to extract the pattern,sub(),gsub() to replace the pattern

Sapmle Code

#To locate the patterns
#To find starting position and length of first match
regexpr(“aeiou”,c(“apple”,”bat”,”cat”,”Crypt”,”dog”,”elephant”,”Flag”,”aeiou”,”AEIOU”))
#To find starting position and length of all matches
gregexpr(“aeiou”,c(“apple”,”bat”,”cat”,”Crypt”,”dog”,”elephant”,”Flag”,”aeiou”,”AEIOU”))
#Extract first match
regmatches(c(“apple”,”bat”,”cat”,”Crypt”,”dog”,”elephant”,”Flag”,”aeiou”,”AEIOU”), regexpr(“[aeiou]”,c(“apple”,”bat”,”cat”,”Crypt”,”dog”,”elephant”,”Flag”,”aeiou”,”AEIOU”)))
#Extracts all matches, outputs a list
regmatches(c(“apple”,”bat”,”cat”,”Crypt”,”dog”,”elephant”,”Flag”,”aeiou”,”AEIOU”), gregexpr(“[aeiou]”,c(“apple”,”bat”,”cat”,”Crypt”,”dog”,”elephant”,”Flag”,”aeiou”,”AEIOU”)))
#To replace the string
#To replace first match
sub(“ve”,”ving”,c(“have”,”Fast”,”drive”,”go”,”have to serve”))
#To replace all matches
gsub(“er”,”s”,c(“ever and ever”,”server”,”faster than ever”,”have to serve”))

Screenshots