Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R grep pattern regex with brackets

Tags:

I have a problem with grep in R:

patterns= c("AB_(1)","AB_(2)") text= c("AB_(1)","DDD","CC")  grep(patterns[1],text) >integer(0)  ???? 

the grep command has problem with "()" brackets, is there any as.XX(patterns[1]) that I can use??

like image 987
user170322 Avatar asked Nov 03 '11 09:11

user170322


1 Answers

You need escape by double backslash:

> patterns= c("AB_\\(1\\)","AB_(2)") > text= c("AB_(1)","DDD","CC") >  > grep(patterns[1],text) [1] 1 
like image 87
kohske Avatar answered Sep 22 '22 04:09

kohske