0

I'm trying to write regular expressions in R to detect a word that can occur anywhere within a string, but only when it occurs as an entire word. e.g. in

samplestr <- c("LT BLAHBLAH", "BLAH LT BLAH", "BLAHLT BLOO")

I want to detect all occurances of "LT" but not cases where a word contains LT within a larger group. Desired output of grepl("regex that works",samplestr) (or similar string detection function) would be:

TRUE TRUE FALSE

samalevy
  • 81
  • 8
  • so basically `str_detect(samplestr , "^LT| LT | LT$")`? – D.J Nov 04 '21 at 09:15
  • 1
    This seems to be an exact duplicate of [this](https://stackoverflow.com/questions/66631361/regex-in-r-to-detect-exact-word) – Mata Nov 04 '21 at 09:17

1 Answers1

0

You may use grep with the regex pattern \bLT\b:

samplestr <- c("LT BLAHBLAH", "BLAH LT BLAH", "BLAHLT BLOO")
output <- grep("\\bLT\\b", samplestr, value=TRUE)
output

[1] "LT BLAHBLAH"  "BLAH LT BLAH"

The pattern \bLT\b has word boundaries on either side of LT, which will only match LT when as a standalone word, or, more generally, when surrounded by non word characters.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360