I am trying to use regular expressions using the stringr package to extract some text. For some reason, I get the error "Invalid regexp". I tried regex expression in some website testing tools and it seems to work there. I was wondering if there is something unique in how the regular expression works in R and especially in the stringr package.
Here is an example:
string <- c("MARKETING: Vice President", "FINANCE: Accountant I", "OPERATIONS: Plant Manager") pattern <- "[AZ]+(?=:)" test <- gsub(" ","",string) results <- str_extract(test, pattern)
This does not work. I would like to get "MARKETING", "FINANCE" and "OPERATIONS" without ":" in them. This is why I use lookahead syntax. I understand that I can just get around this using:
pattern <- "[AZ]+(:)" test <- gsub(" ","",string) results <- gsub(":","",str_extract(test, pattern))
But I expect that I may need to use images for more complex situations than this in the near future.
Do I need to adjust regex with some screens or something to make this work?
source share