[R] partial matching with grep()

David Winsemius dwinsemius at comcast.net
Mon Nov 2 17:01:20 CET 2009


On Nov 2, 2009, at 9:57 AM, Vito Muggeo (UniPa) wrote:

> dear all,
> This is a probably a silly question.
> If I type
> > grep("x",c("a.x" ,"b.x","a.xx"),value=TRUE)
> [1] "a.x"  "b.x"  "a.xx"

 > grep("\\.x\\b",c("a.x" ,"b.x","a.xx"),value=TRUE)
[1] "a.x" "b.x"

Or:
 > grep("\\.x$",c("a.x" ,"b.x","a.xx"),value=TRUE)
[1] "a.x" "b.x"

Why you might ask. You need the double "\\" in order to get the  
interpreter to interpret them as single escapes prior to the period  
and the "\" in "\b", which is the end-of-word match pattern, while "$"  
is an end of sentence pattern.


>
> Instead, I would like to obtain only
> "a.x"  "b.x"
> How is it possible to get this result with grep()?
>
> many thanks for your attention,
> best,
> vito
>
> -- 
> ====================================
> Vito M.R. Muggeo
> Dip.to Sc Statist e Matem `Vianelli'
> Università di Palermo
> viale delle Scienze, edificio 13
> 90128 Palermo - ITALY
> tel: 091 6626240
> fax: 091 485726/485612
> http://dssm.unipa.it/vmuggeo
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

David Winsemius, MD
Heritage Laboratories
West Hartford, CT




More information about the R-help mailing list