0

I want to detect (and then extract) month names from text using str_detect and str_extract.

For this, I create an object containing all month names and abbreviations.

m <- paste(c(month.name, month.abb), collapse = "|")
> m
[1] "January|February|March|April|May|June|July|August|September|October|November|December|Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec"

Then, I want to detect any of the entries occurring as a single word (surrounded by word boundaries):

stringr::str_detect(c("inJan", "Jan"), str_glue("\\b{m}\\b"))

This, however, returns TRUE TRUE (I expect FALSE TRUE, as the first is not a single word.

I suspect this is due to the collapsing of the list, as stringr::str_detect(c("inJan", "Jan"), str_glue("\\bJan\\b")) returns the expected FALSE TRUE.

I need to detect occurrences of m, however. What's the best way to go about this?

Ivo
  • 3,890
  • 5
  • 22
  • 53

0 Answers0