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?

2
  • 1
    Group the alternatives Commented Oct 31, 2019 at 10:31
  • stringr::str_detect(c("inJan", "Jan"), str_glue("\\b({m})\\b")) does the trick. Commented Oct 31, 2019 at 11:54

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.