To locate,extract and replace pattern using regular expression in R
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
#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”))