MathGroup Archive 2009

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

Search the Archive

Finding the Position of Elements in a List that Contain a Substring

  • To: mathgroup at smc.vnet.net
  • Subject: [mg102157] Finding the Position of Elements in a List that Contain a Substring
  • From: Gregory Lypny <gregory.lypny at videotron.ca>
  • Date: Fri, 31 Jul 2009 05:53:36 -0400 (EDT)

Hello everyone,

Suppose I have a list of strings, say, sentences such as

	listOfStrings = {"The cat is here.", "It's not here.",  "Not in the  
catalogue,", "Where is the cat?"}

and a string I want to search for

	searchString = "cat"

I can use StringCases to pick off the elements that contain the search  
string

	StringCases[listOfStrings, __ ~~ searchString ~~ ___]

	{{"The cat is here."}, {}, {"Not in the catalogue,"}, {"Where is the  
cat?"}}

But what if I just want to know the positions of the elements that are  
hits?  In this case, it's

	{1, 3, 4}

If I use

	Position[listOfStrings, ___ ~~ theString ~~ ___]

I get

	{}

which is not what I expect.  Also how can I have my searchString  
treated like it is a word so that 3 is not one of the hits?

Any hints would be much appreciated.

	Gregory


  • Prev by Date: PlayerPro 7.0.1 and Parallel Evaluation
  • Next by Date: Re: Slow start to AstronomicalData[]
  • Previous by thread: PlayerPro 7.0.1 and Parallel Evaluation
  • Next by thread: Re: The audience for Mathematica (Was: Re: Show doesn't work inside