Research breakthrough possible @S-Logix pro@slogix.in

Office Address

  • 2nd Floor, #7a, High School Road, Secretariat Colony Ambattur, Chennai-600053 (Landmark: SRM School) Tamil Nadu, India
  • 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
locate,extract and replace pattern using regular expression in R
To locate pattens
find starting position and length of first match
To find starting point of all pattens