我想检查特殊数字(“ 2020”,整数/年)是否在字符串中出现两次。我试过了,但是没有用。 谁可以帮助我?
grep(pattern = "2020{2}", x = "DataMW_2029__ForecastMW_2020")
谢谢:-)
答案 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