Mathematica 9 is now available
Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2011

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

Search the Archive

Re: Pattern Matching Question

  • To: mathgroup at smc.vnet.net
  • Subject: [mg116743] Re: Pattern Matching Question
  • From: Andrzej Kozlowski <akoz at mimuw.edu.pl>
  • Date: Fri, 25 Feb 2011 06:38:30 -0500 (EST)

Presumably you mean this:

ls = {x^2, x^a, x^(a + b)};

Cases[ls, Except[HoldPattern[x^(_ + _)], x^_], {1}]

{x^2, x^a}

Note that using HoldPattern is crucial here since otherwise: Power[x,Plus[_,_]] evaluates to something you do not want:


 x^(_ + _)

 x^(2*_)

Andrzej Kozlowski




On 24 Feb 2011, at 12:23, Just A Stranger wrote:

> I'm new to pattern matching, and am stuck on a seemingly simple probem:
>
> If I want to transform a pattern like
>
> (I)  Power[x,_]   i.e. x^_
>
> but NOT
>
> (II) Power[x,Plus[_,_]]   i.e. x^(_+_)
>
> How would I go about making that rule?
>
> x^_ -> NewForm
>
> Applies the rule to (II), which I don't want.
>
> Thank you for your help :)
>
>
>


  • Prev by Date: Formatting a Cell Programmatically
  • Next by Date: Re: Pattern Matching Question
  • Previous by thread: Re: Pattern Matching Question
  • Next by thread: Re: Pattern Matching Question