To detect pattern using regular expression in R with stringR package
Load the data file
Convert the data required as a character vector
Use the predefined function to split,detect and locate pattern
library(readtext)
library(stringr)
data #To split the string using a pattern
(str_split(data$text,” “))
#To detect the pattern.Output is a logical vector containing true and false
str_detect(c(“apple”,”bat”,”cat”,”Crypt”,”dog”,”elephant”,”Flag”,”aeiou”,”AEIOU”),”aeiou”)
#To locate the pattern in the string with start and end position for the first match only
str_locate(c(“apple”,”bat”,”cat”,”Crypt”,”dog”,”elephant”,”Flag”,”aeiouaeiou”,”AEIOU”),”aeiou”)
#To locate the pattern in the string with start and end position for all matches
str_locate_all(c(“apple”,”bat”,”cat”,”Crypt-aeiou”,”dog”,”elephant”,”Flag”,”aeiouaeiou”,”AEIOU”),”aeiou”)