0

This may be a simple question to some: how do you actually match nothing using regex in R? Suppose you have a vector of strings such as this:

q <- c("a", "12", "0", "", "300")

And suppose further you want to match the empty string "", how to go about this? It seems one can reasonably well match "" using grep to match the metacharacter . meaning 'any character', either as such or as the content of a negated character class, as well as the argument invert = T to match the opposite of the match:

grep(".", q, value = T, invert = T)
[1] ""

grep("[^.]", q, value = T, invert = T)
[1] ""

In either case the match works. But, surely, using invert feels like a convenient trick but not like a serious regex. Is there another way of matching nothing in R?

Chris Ruehlemann
  • 20,321
  • 4
  • 12
  • 34

1 Answers1

0

One option would be to specify the ^ for start of the string followed by $ as the end of the string

grep("^$", q)

Or with nchar

which(nchar(q) == 0)
akrun
  • 874,273
  • 37
  • 540
  • 662
  • I'm not sure what OP wants to do, but `^$` wouldn't match any of the values in this array `q <- c("a", "12", "0", "300")`. However `.*` should match all and also empty string – Federico Piazza Jun 16 '20 at 19:48
  • @FedericoPiazza. I guess the OP want to match blank i.e `""` in his post directly instead of the `invert = TRUE`. Perhaps the OP would clarify it. thanks – akrun Jun 16 '20 at 19:49
  • 2
    I'm perplexed by the downvote. Maybe the voter misunderstood the question (though "And suppose further you want to match the empty string "", how to go about this?" seems pretty clear) or expected you to say the multiline flag was not set. Who knows? I normally don't upvote answers to trivial questions, but here I've cast a compensatory vote. – Cary Swoveland Jun 16 '20 at 19:54