Silver.Rainbow
Silver.Rainbow

Reputation: 435

Word boundaries in str_detect function in R

In the string below, I want str_detect to return TRUE only if the characters 'AD ' are found in the string.

ocode<-"ADV TXN CODE SCHED CC AMEX"

I tried

str_detect(ocode,pattern="AD") which returns TRUE as expected
str_detect(ocode,pattern="ADV") which also returns TRUE as expected
str_detect(ocode,pattern="AD\b") returns FALSE as expected 

But

str_detect(ocode,pattern="ADV\b") returns FALSE

I don't understand why that ishappening? Should it not find 'ADV' followed by space, and return true?

The problem I'm trying to solve is to filter strings given an input but the filter returns strings with both AD and ADV while I want the filter to return only the string with AD if the search criteria is AD.

Upvotes: 2

Views: 1420

Answers (1)

ladylala
ladylala

Reputation: 221

Try using str_detect(ocode,pattern="ADV\\b"). When using regex in R, you have to escape the \.

And that regex just returns what comes before \\b because it returns everything before the non-word character:

str_extract(ocode,pattern="ADV\\b")
## [1] "ADV"

Upvotes: 3

Related Questions