1

I expected str_detect (v1.0.0) could match anything with the pattern ^.* since the * should match 0 or more times. However, str_detect(string = "", pattern = "^.*") returns FALSE.

Why doesn't it return TRUE, and is there an alternative pattern that would always return TRUE no matter the string (but still using str_detect)?

wdkrnls
  • 4,548
  • 7
  • 36
  • 64
  • According to http://www.regexpal.com/ this search string "matches 0 or more characters" so it throws an error as there are an infinite number of matches possible (regardless of the search space). I don't know if the same thing is true in R, but that'd be my guess. – Draco18s no longer trusts SE Jan 06 '16 at 20:23
  • That page returns "infinite" for a lot of perfectly valid regexes. – Turn Jan 06 '16 at 20:24
  • 1
    I tried `str_detect(string = "", pattern = "^$")` which also returns `FALSE`. I would assume there is an early out somewhere hidden in the code. Which makes it impossible to use the empty String. Especially, as `str_detect(string = "ab", pattern = "a.*b")` returns `TRUE` – CAFEBABE Jan 06 '16 at 20:35
  • From what I know you should be safe with `str_detect(string = "", pattern = "^.*$")` - just use both the anchors. – Wiktor Stribiżew Jan 06 '16 at 20:45
  • @stribizhev: I just tried your expression and it returned `FALSE`. – wdkrnls Jan 06 '16 at 20:47
  • I ended up writing a wrapper around `str_detect` to check such cases with an or expression `... | string == ""`. – wdkrnls Jan 06 '16 at 20:49
  • 1
    Is there actually a good reason why you are not using `grep(".*","")` – CAFEBABE Jan 06 '16 at 20:50
  • @CAFEBABE: Nope, if `grep` can return a `logical` directly. – wdkrnls Jan 06 '16 at 20:53
  • 1
    @wdkrnls this seems like a bug. Report on github, perhaps? – Matthew Plourde Jan 06 '16 at 20:55
  • what are you trying to achieve? It seems you need just `is.character("")`. – Andrey Jan 06 '16 at 20:58
  • 3
    @wdkrnls `grepl()` is the version of `grep()` that returns a logical result – Rich Scriven Jan 06 '16 at 21:08

1 Answers1

1

(As an answer to not clutter the comment section. However, I have the feeling I'm still missing something)

If the only constraint is that the return value is a logical. You can use

grepl(".*","")
[1] TRUE
CAFEBABE
  • 3,983
  • 1
  • 19
  • 38
  • I'm really wondering why this got down voted. Based on the discussions this looks like a proper solution to me. – CAFEBABE Jan 06 '16 at 21:07
  • If you change your code to use `grepl` I'll accept it. That way I don't have to define a new function (I didn't downvote it, though). – wdkrnls Jan 06 '16 at 21:13
  • Despite the discussion the original question still says the answer should use `str_detect`. Maybe that's the downvote. – Turn Jan 07 '16 at 22:02