MathGroup Archive 2005

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

Search the Archive

Re: StringCases

  • To: mathgroup at
  • Subject: [mg56915] Re: [mg56903] StringCases
  • From: Andrzej Kozlowski <akoz at>
  • Date: Tue, 10 May 2005 03:42:17 -0400 (EDT)
  • References: <>
  • Sender: owner-wri-mathgroup at

On 9 May 2005, at 14:46, Maxim wrote:

> It seems that the string matching functions don't handle patterns in
> exactly the same way as the ordinary pattern matching Mathematica
> functions:
> In[1]:= StringCases["ab", s_ /; s === "a" :> s]
> Out[1]= {"a"}
> In[2]:= StringCases["ab", s_ :> s /; s === "a"]
> Out[2]= {"a" /; "a" === "a", "b" /; "b" === "a"}
> I think that it isn't mentioned anywhere in the documentation that
> StringCases/StringReplace don't understand the conditions on the
> right-hand side of the rule.

I don't find it very surprising since over the years I have repeatedly 
heard form "well informed sources" that one should place conditions as 
close as possible  to the pattern that they apply to and that 
conditions on the right are supported only for reasons of compatibility 
with early versions of Mathematica (I think in version 1 you had to put 
conditions on the right). This is actually stated in more than one book 
on Mathematica although probably not in the documentation.

> In[3]:= StringCases["abcd", (s_ ~~ "b") | (s_ ~~ "d")]
> Out[3]= {"ab"}
> This one is in plain disagreement with the standard pattern matching
> rules, as in e.g. Cases[{f[a, b], f[c, d]}, f[s_, b] | f[s_, d]].

But this works

StringCases["abcd", (s_ ~~ "b"|"d") ]



Andrzej Kozlowski

  • References:
  • Prev by Date: function definition difficulty
  • Next by Date: Re: Problem with substitutions in SparseArray?
  • Previous by thread: StringCases
  • Next by thread: ArcTan[1/0] no result, but ArcTan[Infinity] ok. How to resolve?