2

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.

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.

Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
Adrian
  • 9,229
  • 24
  • 74
  • 132
  • I'm not sure what the synatax for R is, but you could try `^\s*$` which is 0 or more whitespace characters, which should capture blank lines. – Abbath Nov 02 '14 at 21:42
  • Thanks, but that didn't quite work. – Adrian Nov 02 '14 at 21:44
  • 1
    What does the data look like. Is it a vector? Data.frame etc. Please make this reproducible. – Tyler Rinker Nov 02 '14 at 21:44
  • http://stackoverflow.com/questions/3012788/how-to-check-if-a-line-is-blank-using-regex – KFB Nov 02 '14 at 21:49
  • I tried `grep("^s*$", string)` but that didn't work. `string = c("Precedence: bulk", "List-Unsubscribe: ", "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. – Adrian Nov 02 '14 at 21:53
  • `grep("^\\s$",string)` works. You need the double-backslash in R to escape the backslash in the regexp ... – Ben Bolker Nov 02 '14 at 22:08

1 Answers1

4

In R you need to escape the backslash so it should be ^\\s*$. You can also use ^[[:space:]]*$.

grepl("^[[:space:]]*$", c("", "  ", "\t"))
# [1] TRUE TRUE TRUE
grep("^[[:space:]]*$", string)
# [1] 4 8
flodel
  • 87,577
  • 21
  • 185
  • 223