there, I'm trying to extract the text before the number, including it, using regex in R.
As an example:
string <- "Fallen tree at Barth Avenue nº 34. Center Cause Effect (CCE) #omg"
Desired result: "Fallen tree at Barth Avenue nº 34"
I have found some ways to do this task without including the number, but that is not what I need.
sub(pattern='[0-9]+.*', replacement='', x=string)
"Fallen tree at Barth Avenue nº "
thks in advance
答案 0 :(得分:0)
我们可以捕获以零或多个非数字开头的字符(^
),后跟一个或多个数字作为一个组后跟其他字符,并将其替换为反向引用(\\1
)捕获组
sub("^([^0-9]*\\d+).*", "\\1", string)
#[1] "Fallen tree at Barth Avenue nº 34"