[R] Regular expressions
Ales Ziberna
aleszib at gmail.com
Wed Jan 11 19:12:30 CET 2006
Matching regular expressions
Dear useRs!
I have the following problem. I would like to find objects in my environment
that have two strings in it. For example, I might want to find objects that
have in their names "MY" and "TARGET". I do not care about the ordering of
these two substrings in the name, neither what is in front, behind or
between them, the only thing important is that both words are present. I
apologize if this is covered in help pages (then I did not understand it by
reading them several times) or it was answered previously (then I did not
find it).
Since "ls" with argument pattern essentially uses "grep" (if I am not
mistaken), I have an example for "grep"
text<-c("somethigMYsomthing elseTARGET another thing","MY somthing TARGET
another thing","somethig somthing elseTARGETMY another
thing","somethigMTARGETY another thing")
grep(pattern="MY&TARGET", x=text)
#I would like to get 1 2 3 and not 4 or actually their names using
text[grep(pattern="MY&TARGET", x=text)]
#of course, the "pattern" in this case is wrong
I know I can do
text[grep(pattern="MY", x=text)][grep(pattern="TARGET",
x=text[grep(pattern="MY",x=text)])]
However I hope there exists a more elegant way.
Thanks in advance for any suggestions!
Best,
Ales Ziberna
More information about the R-help
mailing list