Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Backreferencing in R (Regular Expressions)

Tags:

regex

r

I'm not sure why I can't get a simple back reference to work in R/RStudio.

grepl('name\1','namename') returns FALSE. grepl('(name)\1','namename') is no good either. What am I doing wrong?

Thanks!

like image 297
monkeybiz7 Avatar asked May 10 '14 16:05

monkeybiz7


1 Answers

Use the double backlash before 1 (the regex engine will understand it as a single backslash):

grepl('(name)\\1', 'namename')
## [1] TRUE

This is because:

cat('(name)\\1')
## (name)\1

In your case, \1 == \001 means an ASCII character of code 1.

charToRaw('\1')
## [1] 01
like image 166
gagolews Avatar answered Oct 29 '22 21:10

gagolews