R中的RegEx以识别数字出现的频率

时间:2019-10-14 15:20:54

标签: r regex

我想检查特殊数字(“ 2020”,整数/年)是否在字符串中出现两次。我试过了,但是没有用。 谁可以帮助我?

grep(pattern = "2020{2}", x = "DataMW_2029__ForecastMW_2020")

谢谢:-)

2 个答案:

答案 0 :(得分:3)

您可以使用gregexpr测试2020是否出现两次:

length(gregexpr("2020", "DataMW_2029__ForecastMW_2020")[[1]]) == 2
#[1] FALSE

length(gregexpr("2020", "DataMW_2020__ForecastMW_2020")[[1]]) == 2
#[1] TRUE

或者通过regex测试2个或更多。

grepl("(.*2020){2}", "DataMW_2029__ForecastMW_2020")
#[1] FALSE

grepl("(.*2020){2}", "DataMW_2020__ForecastMW_2020")
#[1] TRUE

或精确匹配2次:

grepl("^(?!(.*2020){3})(.*2020){2}.*$", "DataMW_2029__ForecastMW_2020", perl=TRUE)
#[1] FALSE
grepl("^(?!(.*2020){3})(.*2020){2}.*$", "DataMW_2020__ForecastMW_2020", perl=TRUE)
#[1] TRUE
grepl("^(?!(.*2020){3})(.*2020){2}.*$", "DataMW_2020__ForecastMW_2020_2020", perl=TRUE)
#[1] FALSE

答案 1 :(得分:2)

我会使用stringr::str_count()

x <- c("DataMW_2029__ForecastMW_2020", "DataMW_2020__ForecastMW_2020")
stringr::str_count(string = x, pattern = "2020")
# [1] 1 2
stringr::str_count(string = x, pattern = "2020") == 2
# [1] FALSE  TRUE