我尝试使用gsub() R函数通过模式提取子字符串。
正如您所看到的,使用gsub()很容易排除所需的子字符串,但我需要反转结果并仅得到"7 years"。我考虑使用"^"之类的东西:
gsub(pattern="[^[0-9]+\\s+\\w+]", replacement="", string)
请问是否有人能帮我提供正确的正则表达式模式?
# 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"
正如您所看到的,使用gsub()很容易排除所需的子字符串,但我需要反转结果并仅得到"7 years"。我考虑使用"^"之类的东西:
gsub(pattern="[^[0-9]+\\s+\\w+]", replacement="", string)
请问是否有人能帮我提供正确的正则表达式模式?