tpepler
tpepler

Reputation: 63

Regular expression error when using stringr (R package) to search for curly brackets

I am trying to search for curly brackets in text strings in R, using the stringr package. Using the following code:

library(stringr)
textstring <- 'abc}defg}hij'
str_locate_all(textstring, 'e')

works fine, but

str_locate_all(textstring, '}')

gives the following error message:

Error in stri_locate_all_regex(string, pattern, omit_no_match = TRUE, : Syntax error in regexp pattern. (U_REGEX_RULE_SYNTAX)

I am using R version 3.2.1 and stringr version 1.0.0 in Ubuntu 14.04 LTS.

Can anyone help me, please?

Upvotes: 6

Views: 21478

Answers (2)

Leo
Leo

Reputation: 495

Thank you for the clue and done like follows to replace all the + from spaces:

str_replace_all(df$Installs, "\\+", " ")

Upvotes: 0

lukeA
lukeA

Reputation: 54247

{ is a special character - you have to escape it:

str_locate_all(textstring, '\\}')

Upvotes: 17

Related Questions