Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

regex

r

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?

like image 356
tpepler Avatar asked Dec 08 '22 02:12

tpepler


1 Answers

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

str_locate_all(textstring, '\\}')
like image 175
lukeA Avatar answered Apr 21 '23 16:04

lukeA