我想我可以使用str_extract_all
或在tidyverse的东西,但我不确定如何得到它,因为我的字符串返回是不正确的。
这是字符串:
str <- "12, 47, 48 The integers numbers are also interesting: 189 2036 314 ',' is a separator, so please extract these numbers 125,789,1450 and also these 564,90456. 7890$ per month "
我们可以使用str_extract_all
来提取一个或多个数字(\d+
)的多个实例。输出将是长度为1的list
。因此,我们用[[
list
元素library(stringr)
str_extract_all(string1, "\d+")[[1]]
与产出
[1] "12" "47" "48" "189" "2036" "314" "125" "789" "1450" "564" "90456" "7890"
对于基本R选项,我们可以使用regmatches
和gregexpr
:
regmatches(string1, gregexpr("\d+", string1))
[1] "12" "47" "48" "189" "2036" "314" "125" "789" "1450" "564" "90456" "7890"