[R] Exact matching with grep

Peter Dalgaard P.Dalgaard at biostat.ku.dk
Mon Feb 5 14:36:40 CET 2007


Vladimir Eremeev wrote:
> Mahieux Dimitri wrote:
>   
>> I would know if it is possible with grep to match a exact string. For 
>> example, I want to match the string "DP2" (and only this) and grep match 
>> "DP2BS" too.
>> I have sought in the grep help but I didn't find what I want.
>>
>>     
>
> grep("DP2", {other arguments}, fixed=TRUE)
>
> ?grep says that "fixed is logical. If TRUE, pattern is a string to be
> matched as is. Overrides all conflicting arguments."
>   
That's not the issue here:

> grep("DP2", c("DP2","x", "DP2BS","y"))

[1] 1 3

> grep("DP2", c("DP2","x", "DP2BS","y"), fixed=TRUE)

[1] 1 3
> grep("^DP2$", c("DP2","x", "DP2BS","y"))
[1] 1
> which("DP2"==c("DP2","x", "DP2BS","y"))
[1] 1



"fixed=TRUE" avoids special interpretation of metacharacters, but that
doesn't solve the problem of matching the entire string.

-- 
   O__  ---- Peter Dalgaard             Øster Farimagsgade 5, Entr.B
  c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
 (*) \(*) -- University of Copenhagen   Denmark          Ph:  (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)                  FAX: (+45) 35327907



More information about the R-help mailing list