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" 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
Try grep( "\\.x$", c("a.x" ,"b.x","a.xx"),value=TRUE) The $ means end-of-line (while ^ means start-of-line). And special characters like dot needs to be escaped twice. Regards, Adai 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" > > 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 >
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
Is this what you want: depends on it occurring at the endo of the string:> grep("\\.x[^x]*$",c("a.x" ,"b.x","a.xx"),value=TRUE)[1] "a.x" "b.x" On Mon, Nov 2, 2009 at 9:57 AM, Vito Muggeo (UniPa) <vito.muggeo at unipa.it> 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" > > 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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve?