Skip to content

partial matching with grep()

4 messages · vito muggeo, Adaikalavan Ramasamy, David Winsemius +1 more

#
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
#
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:
#
On Nov 2, 2009, at 9:57 AM, Vito Muggeo (UniPa) wrote:

            
> 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.
David Winsemius, MD
Heritage Laboratories
West Hartford, CT
#
Is this what you want:  depends on it occurring at the endo of the string:
[1] "a.x" "b.x"


On Mon, Nov 2, 2009 at 9:57 AM, Vito Muggeo (UniPa)
<vito.muggeo at unipa.it> wrote: