str_detect not case sensitive

Hi,
I'm looking at str_detect documentation here: https://www.rdocumentation.org/packages/stringr/versions/1.4.0/topics/str_detect

and I cannot find any option of ignoring upper or lower case.

I use this code instead but I find it silly:

library(stringr)
text <- c("No", "no", "something", "Something")
str_detect(text, regex("no|No|Something|something"))

any ideas?

I already told you how to solve this on one of your previous questions but here it is to refresh your memory.

library(stringr)

text <- c("No", "no", "something", "Something")

str_detect(text, regex("no|something", ignore_case = TRUE))
#> [1] TRUE TRUE TRUE TRUE
1 Like

I think you can wrap your regex in regex(): https://www.rdocumentation.org/packages/stringr/versions/1.4.0/topics/modifiers

@andresrcs got there just before me!

I know you have replied to my previous question but my understanding is that regex is used to find sentences including specific words whereas I am trying to find sentences including "no" or "something" exclusively (not a part of a sentence) so sentence "no" should be detected but "no sentence" should not...

Regex is for defining a regular expression not specific words so you just have to use the right regex and BTW you were only asking for case sensitive regular expressions

case sensitive regular expressions but as modification of:

 str_detect(FComm, "no|No|Something|something")

so detecting sentences like "no" or "something".
I could not find it in the str_detect documentation.

Perhaps, I am wrong about regex but using:

str_detect(text, regex("no|something", ignore_case = TRUE))

detects all sentences including "no" o "no comment" will be detected. It shouldn't.

Please, correct me if I'm wrong....

This a good example of why is a good idea to provide a reproducible example for your issue, your request wasn't clear enough, also you are not going to find that in the documentation because your actual problem is that you are not using the correct regular expression and documentation can't help you with that.

I have also already told you how to match single words on a previous answer, but again to refresh your memory.

library(stringr)

text <- c("No", "no", "something", "Something", "no sentence")

str_detect(text, regex("^(no|something)$", ignore_case = TRUE))
#> [1]  TRUE  TRUE  TRUE  TRUE FALSE
3 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.