在那里,我正在尝试使用 R 中的正则表达式提取数字之前的文本,包括它。
举个例子:
string <- "Fallen tree at Barth Avenue nº 34. Center Cause Effect (CCE) #omg"
期望结果:"Fallen tree at Barth Avenue nº 34"
我已经找到了一些方法来完成这项任务,而不包括数字,但这不是我所需要的。 sub(pattern='[0-9]+.*', replacement='', x=string)
"Fallen tree at Barth Avenue nº "
提前致谢
我们可以捕获以零个或多个非数字开头的字符(^
),后跟一个或多个数字作为一组后跟其他字符,并将其替换为捕获组的反向引用(\1
)
sub("^([^0-9]*\d+).*", "\1", string)
#[1] "Fallen tree at Barth Avenue nº 34"