Re: [R] Regular Expressions

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

Sangick Jeon <sijeon <at> ucdavis.edu> 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:

   data(state)
   grep("i.*n|n.*i", state.name) # 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"), state.name) # states with i, n and g in name



R-help@stat.math.ethz.ch mailing list
https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html 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