I am trying to extract a substring from a pattern using the gsub () R function.
# Example: extracting "7 years" substring.
string <- "Psychologist - 7 years on the website, online"
gsub(pattern="[0-9]+\\s+\\w+", replacement="", string)
[1] "Psychologist - on the website, online"
As you can see, it is easy to exclude the desired substring using gsub (), but I need to invert the result and get only "7 years". I am thinking of using "^", something like this:
gsub(pattern="[^[0-9]+\\s+\\w+]", replacement="", string)
Please can someone help me with the correct regex pattern?
source
share