MathGroup Archive 2009

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

Search the Archive

Re: How to get position of string in a mixed list? (string pattern in

  • To: mathgroup at smc.vnet.net
  • Subject: [mg96217] Re: How to get position of string in a mixed list? (string pattern in
  • From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
  • Date: Mon, 9 Feb 2009 05:37:18 -0500 (EST)
  • Organization: The Open University, Milton Keynes, UK
  • References: <gmndli$s1k$1@smc.vnet.net>

In article <gmndli$s1k$1 at smc.vnet.net>,
 "goodhei8 at gmail.com" <goodhei8 at gmail.com> wrote:

> for list like {"a","abc","dfe","nab",1,2,3,{1,2}}, I want to get all 
> strings' position in the list which match a pattern (like 
> ___~~"ab"~~___). How to do it? How to use a string pattern in a list 
> pattern? how to use a pattern with a condition (both list pattern&/; 
> StringQ[#] and list pattern /;StringQ[]&, no working for me)

StringMatchQ[] is the answer as in,

In[1]:= lst = {"a", "abc", "dfe", "nab", 1, 2, 3, {1, 2}};

In[2]:= Cases[lst, s_String /; StringMatchQ[s, ___ ~~ "ab" ~~ ___]]

Out[2]= {"abc", "nab"}

In[3]:= Position[lst, 
 s_String /; StringMatchQ[s, ___ ~~ "ab" ~~ ___]]

Out[3]= {{2}, {4}}

Regards,
--Jean-Marc


  • Prev by Date: Re: I have an operator, How do I DSolve it?
  • Next by Date: Re: I have an operator, How do I DSolve it?
  • Previous by thread: Re: How to get position of string in a mixed list? (string pattern in
  • Next by thread: I have an operator, How do I DSolve it?