MathGroup Archive 2008

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Searching for multiple strings in a list

  • To: mathgroup at
  • Subject: [mg86301] Re: [mg86280] Searching for multiple strings in a list
  • From: Oyvind Tafjord <tafjord at>
  • Date: Sat, 8 Mar 2008 05:39:11 -0500 (EST)
  • References: <>

Coleman, Mark wrote:
> Greetings,
> I'm doing some work with very long lists of strings. As part of the
> effort I need to identify strings that contain all of set of keywords
> (regardless of order) that a user may specify. The function StringCases
> will take multiple keywords as an argument, but it will return a list
> giving those elements that contain any one of the keywords, not *all* of
> the keywords. I know this can be done using multiple calls to
> StringCases and comparing the results for each keyword, but this seems
> very cumbersome, particularly if there are many keywords specified.
> Any help would be most appreciated.
> Thanks,
> -Mark

Here's a reasonably efficient way:

list = {"foo foo", "foo bar", "bar foo none",
    "bar foo bar none", "foobar"};
keywords = Union[{"foo", "bar"}];
  Union /@ StringCases[list,
    WordBoundary ~~ keywords ~~ WordBoundary], keywords]

Out[46]= {"foo bar", "bar foo none", "bar foo bar none"}

Taking advantage of the List support in the first argument of StringCases 
can save a fair amount of time for large lists.

Oyvind Tafjord
Wolfram Research

  • Prev by Date: Re: on Export and AnimationRepetitions
  • Next by Date: Re: Methods for plotting
  • Previous by thread: Searching for multiple strings in a list
  • Next by thread: ResetMenusPacket in version 6