【发布时间】:2017-10-26 10:26:24
【问题描述】:
我尝试使用 gsub() R 函数按模式提取子字符串。
# 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 年”。 我考虑使用“^”,类似这样的:
gsub(pattern="[^[0-9]+\\s+\\w+]", replacement="", string)
拜托,谁能帮助我正确的正则表达式模式?
【问题讨论】:
-
伙计们,你能解释一下为什么在 'replacement="\\1"' 中使用 "\\1" 吗?