Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 逻辑测试字符串是否正好是2个或更多空格_Regex_R - Fatal编程技术网

Regex 逻辑测试字符串是否正好是2个或更多空格

Regex 逻辑测试字符串是否正好是2个或更多空格,regex,r,Regex,R,我希望能够测试字符串,如果它是: 由2个或更多字符组成 所有字符都是空格 因此,我们可以说: x <- c(" ", " p", "\\s\\s", "\\s", " ", "d", " ", " ", ": ") 我从以下几点开始使用: log_test <- gregexpr(":", gsub("\\s", ":", x)) log\u test试试看 > x2 <- gsub('\\\\s',' ',x) > grepl('[

我希望能够测试字符串,如果它是:

  • 由2个或更多字符组成
  • 所有字符都是空格
  • 因此,我们可以说:

    x <- c("   ", "   p", "\\s\\s", "\\s", " ", "d", "  ", "          ", ": ")
    
    我从以下几点开始使用:

    log_test <- gregexpr(":", gsub("\\s", ":", x))
    
    log\u test试试看

    > x2 <- gsub('\\\\s',' ',x)
    > grepl('[[:blank:]]{2,}',x2) & !grepl('[[:alnum:][:punct:]]',x2)
    [1]  TRUE FALSE  TRUE FALSE FALSE FALSE  TRUE  TRUE FALSE
    
    另一种选择:

    grepl('^( |\\\\s){2,}$', x)
    #[1]  TRUE FALSE  TRUE FALSE FALSE FALSE  TRUE  TRUE FALSE
    

    效果很好,我会暂缓批改,看看是否有其他答案+1.
    > x2 <- gsub('\\\\s',' ',x)
    > grepl('[[:blank:]]{2,}',x2) & !grepl('[[:graph:]]',x2)
    [1]  TRUE FALSE  TRUE FALSE FALSE FALSE  TRUE  TRUE FALSE
    
    grepl('^( |\\\\s){2,}$', x)
    #[1]  TRUE FALSE  TRUE FALSE FALSE FALSE  TRUE  TRUE FALSE