Re: question on Cases with elements that might be strings
- To: mathgroup at smc.vnet.net
- Subject: [mg86554] Re: question on Cases with elements that might be strings
- From: Szabolcs <szhorvat at gmail.com>
- Date: Thu, 13 Mar 2008 20:50:56 -0500 (EST)
- References: <frasc3$1hm$1@smc.vnet.net>
On Mar 13, 10:32 am, ramiro <ramiro.barran... at uvm.edu> wrote: > Hi, > > The following is a valid pattern match: > > In[96]:= StringMatchQ["s1", StringExpression["s", NumberString]] > > Out[96]= True > > HOWEVER, the following does not work: > In[89]:= Cases[{{1 -> "s1", 1}, {2 -> "s2", 2}}, {_ -> > StringExpression["s", NumberString], _}] > > Out[89]= {} > > Even though this works: > In[95]:= Cases[{{1 -> "s1", 1}, {2 -> "s", 2}}, {_ -> > StringExpression["s"], _}] > > Out[95]= {{2 -> "s", 2}} > > How come cases does not work on ln[89]? > > Ramiro StringExpression cannot be used with Cases. Generally, string patterns and expression patterns cannot be mixed. In[95] doesn't "work" either. It returns the result you expected only because StringExpression["s"] was immediately evaluated to "s". So, in In[85], change the StringExpression[ ... ] to s_String /; StringMatchQ[s, "s" ~~ NumberString], and it'll work fine.