Re: [R] Regular Expressions

From: Gabor Grothendieck <>
Date: Tue 13 Jul 2004 - 11:41:30 EST

Sangick Jeon <sijeon <at>> writes:

> Is there a way to use regular expressions to capture two or more words in a
> sentence? For example, I wish to to find all the lines that have the
words "thomas",
> "perl", and "program", such as "thomas uses a program called perl", or "perl
is a
> program that thomas uses", etc.

If you only have two patterns to search for then a regular expression can be done this way:

   grep("i.*n|n.*i", # states with i and n in name

but it gets unwieldy if you have three since there are 6 permutations, not 2. In that case, you are probably better off iterating greps like this:

   lookfor <-
   function(pat, x) { for(p in pat) x <- grep(p, x, value = TRUE); x }

   lookfor(c("i","n","g"), # states with i, n and g in name mailing list PLEASE do read the posting guide! Received on Tue Jul 13 11:53:03 2004

This archive was generated by hypermail 2.1.8 : Wed 03 Nov 2004 - 22:54:54 EST