-1

I'm pretty new to regex and am trying to detect a word with the "+" symbol when surrounded by "\\b" in long strings of words but both stringr and grepl are giving me the wrong result.

This is the code that I have wrote:

library(stringr)
str_detect("coversyl +", "\\bcoversyl(plus| plus|\\+| \\+)\\b")

The output is FALSE which is wrong.

What would be the right way to do it?

EdTeD
  • 63
  • 7
  • Related to [this](https://stackoverflow.com/questions/41174959/why-does-is-this-end-of-line-b-not-recognised-as-word-boundary-in-stringr-ic) – akrun Jun 26 '19 at 03:58
  • It is exactly this. The word boundary should not be used next to a non-word char if it must match when enclosed with non-word chars/start (end) of string. If you need unambiguous word boundaries use `(?<!\w)` / `(?!\w)` lookarounds: `"(?<!\\w)coversyl(plus| plus|\\+| \\+)(?!\\w)"` – Wiktor Stribiżew Jun 26 '19 at 07:54

1 Answers1

0

My guess is that your expression is just fine, maybe missing an space,

\\bcoversyl\\b\\s(\\bplus\\b|\\+)

Please see the demo for additional explanation.

If we might want more than one space, we would simply change \\s to \\s+ and it might work:

\\bcoversyl\\b\\s+(\\bplus\\b|\\+)
Emma
  • 27,428
  • 11
  • 44
  • 69