tomw
tomw

Reputation: 3158

Regex in R (str_detect)

What must be a painfully obvious error for an expert: how do I match the location of the bracket digit pattern in the following strings with stringr?

library(stringr)

s <- c("ser ser (1 ( asd",
       "ser (3 (. asd",
       "ser ser (1 (2 asd")

I want to match the pattern "(", then any digit. I think the right regex is "\(\d" but this command

str_detect(s[1], "\(\d")

provides

Error: '\(' is an unrecognized escape in character string starting ""\("

What's the right way to write this regular expression?

Upvotes: 0

Views: 1098

Answers (1)

MrFlick
MrFlick

Reputation: 206187

You need to escape slashes in R strings

str_detect(s[1], "\\(\\d") 

Upvotes: 3

Related Questions