Grep 函数:如何组合字符和一系列数字

Grep-function: How to combine characters and a range of numbers

我想使用 grep 函数从数据框中获取特定项目。所有感兴趣的项目都以 "ffcb2" 开头,但我只想 select 那些以数字 07 到 16 结尾的项目。但我不知道如何在命令中正确包含此范围。到目前为止,它看起来像这样:

grep(names_data, pattern = "ffcb2[07-16]", value = TRUE)

要匹配从 716 的数字,您可以使用 numeric range regex generator 来获得以下模式:

(0[7-9]|1[0-6])

所以,你的命令看起来像

grep(names_data, pattern = "ffcb2(0[7-9]|1[0-6])", value = TRUE)

regex demo(0[7-9]|1[0-6]) 匹配 0 后跟 789,或者 1 后跟从 06.