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
返回这些索引。
答案 0 :(得分:4)
在R中你需要转义反斜杠,因此它应该是^\\s*$
。您也可以使用^[[:space:]]*$
。
grepl("^[[:space:]]*$", c("", " ", "\t"))
# [1] TRUE TRUE TRUE
grep("^[[:space:]]*$", string)
# [1] 4 8