[R] Parenthesis recognition with grep

Charilaos Skiadas cskiadas at gmail.com
Sun Aug 31 17:24:31 CEST 2008


myStr <- "YD\\(001\\)"

In POSIX format, or in most such formats in fact, special characters  
like parentheses have a particular meaning, and need to be escaped if  
they are to have the "parenthesis" meaning. This is done typically by  
putting a backslash in front of them. Since however a backslash has a  
special meaning inside a "...", namely it is use to "escape" things,  
we need to escape it, hence the two backslashes you see back to back.  
You can use:


To see what hte string actually has in it.

A better way to solve your problem in your case is to use the  
original myStr you had, but change the grep call to be:

grep(myStr, headers, fixed=TRUE)

The fixed=TRUE part tells it to treat myStr as a string to be matched  
exactly. Of course ?grep should probably have led you to this.

Haris Skiadas
Department of Mathematics and Computer Science
Hanover College

On Aug 31, 2008, at 10:54 AM, Sébastien wrote:

> Hi Jorge,
> This is doing the work just fine. Thank you !
> However, I would like to know what should be done with the grep  
> call...
> just for my personal education :)
> Sebastien
> Jorge Ivan Velez a écrit :
>> Dear Sébastien,
>> Is this what you want?
>> which(myStr==headers)
>> [1]  2
>> which(headers%in%myStr)
>> [1] 2
>> HTH,
>> Jorge
>> On Sun, Aug 31, 2008 at 10:28 AM, Sébastien <pomchip at free.fr
>> <mailto:pomchip at free.fr>> wrote:
>>     Dear R-users,
>>     I need to dynamically recognize the index of a given string myStr
>>     in a vector of string. The problem is that myStr might contain
>>     parenthesis, causing grep not to recognize it the way I want (see
>>     below). The help mentions that the pattern used by grep should be
>>     in the POSIX format... I guess the problem is here;  
>> unfortunately,
>>     I am not familiar with the subtleties of the POSIX format
>>     ex:
>>     myStr <- "YD(001)"
>>     headers <-c("TD", "YD(001)", "YD(002)", "T", "Y(001)", "Y(002)")
>>     grep(myStr, headers)
>>     How should I modify my grep call to get the match right?
>>     Thank you for your help,
>>     Sebastien

More information about the R-help mailing list