空行的正则表达式是什么?

时间:2014-11-02 21:38:56

标签: regex r

http://www.endmemo.com/program/R/grep.php

我已经在图表中查找了与空白行相对应的正则表达式,但我似乎无法找到它。我试过\\n,但这意味着换行,而不是它必然是空白。

示例

string = c("Precedence: bulk", 
  "List-Unsubscribe: <mailto:zzzzteana-unsubscribe@yahoogroups.com>", 
  "Content-Transfer-Encoding: 7bit", 
  "", 
  "Martin A posted:", 
  "Tassos Papadopoulos, the Greek sculptor behind the plan, judged that the", 
  " Mount Athos monastic community, was ideal for the patriotic sculpture. ", 
  " ", 
  " As well as Alexander's granite features, 240 ft high and 170 ft wide, a",
  "planned", 
  "---------------------", 
  "So is this mountain limestone or granite?")

因此第4行和第8行是空的,我希望grep返回这些索引。

1 个答案:

答案 0 :(得分:4)

在R中你需要转义反斜杠,因此它应该是^\\s*$。您也可以使用^[[:space:]]*$

grepl("^[[:space:]]*$", c("", "  ", "\t"))
# [1] TRUE TRUE TRUE
grep("^[[:space:]]*$", string)
# [1] 4 8