简体   繁体   English

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

[英]what is the regular expression for a blank line?

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

I've looked through the chart for a regular expression that corresponds to a blank line, but I just can't seem to find it. 我已经在图表中查看了与空白行相对应的正则表达式,但似乎找不到它。 I've tried \\\\n but that means a newline, not that it's necessarily blank. 我已经尝试过\\\\n但这意味着要换行,而不一定是空白。

example : 例如

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?")

So lines 4 and 8 are empty, and I want grep to return those indices. 因此,第4行和第8行为空,我希望grep返回这些索引。

In R you need to escape the backslash so it should be ^\\\\s*$ . 在R中,您需要转义反斜杠,所以它应该是^\\\\s*$ You can also use ^[[:space:]]*$ . 您也可以使用^[[:space:]]*$

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM