Detecting exact matches using str_detect() in R wasn't able to provide clear solution for me.
Suppose I have
test <- c("HR", "p-value (stratified)", "HRf", "HR-fake", "p-value", "p-value (unstratified)")
want <- c(TRUE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE)
The best way would be just to simply
> test == "HR" | test == "p-value (stratified)"
[1] TRUE TRUE FALSE FALSE FALSE FALSE
but for the sake of learning, I wish to do it in regex. However, none of these worked for me.
> str_detect(testvec, "HR|p-value (stratified)")
[1] TRUE FALSE TRUE TRUE FALSE FALSE
> str_detect(testvec, "\\bHR\\b|\\bp-value (stratified)\\b")
[1] TRUE FALSE FALSE TRUE FALSE FALSE
It seems the problem is that str_detect() is
Detecting "HR-fake" even with "\bHR\b"
str_detect("HRf","\\bHR\\b")
1 FALSEstr_detect("HR-fake","\\bHR\\b")
1 TRUEstr_detect("HR - fake","\\bHR\\b")
1 TRUENot detecting "p-value (stratified)" even with "p-value (stratified)"
str_detect("p-value (stratified)","p-value (stratified)")
1 FALSE
What are causing the issue here? Thank you.