Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Imagine I have an expression like

a*k + (a^2)*b*c + b*e

and I would like to obtain the term containing, for example, some power of a. In that case I would extract the two first terms. If I wanted to obtain the term b*e, I would extract the last.

How can I do it? I guess I should use Cases or something similar, but I don't understand its syntax.

share|improve this question
Maybe Most[CoefficientList[a*k + (a^2)*b*c + b*e, a]] – Daniel Lichtblau Jan 25 '13 at 16:39

You can use pattern matching. For instance to match a which is multiplied by something else you use

Cases[a*k + (a^2)*b*c + b*e, a*_]


Cases[a*k + (a^2)*b*c + b*e, a^_, 2]

The 2 is necessary because a^2 appears deeper in you expression. Furthermore, to prevent trouble, you should be aware of FullForm which shows you the full underlying representation of your structure.

If something does not work, check the FullForm of you pattern and the thing you want to match first. You should start by

Introduction to Patterns


The first part of my answer was not a ready to use solution. It assumed, that you do some work for yourself: Finding the patterns which help you to extract the parts you want.

Now, it seems you are asking something different: When you look at your expression as a polynomial in a, then it's worth looking at functions like CoefficientList (as already mentioned by Daniel in the comment), Coefficient, ... Please look here to have an overview

Polynomial algebra

With this you get the expression containing some power (even the one with a^0) easily

CoefficientList[a*k + (a^2)*b*c + b*e, a]

Or you can pick one special power, like the second one

Coefficient[a*k + (a^2)*b*c + b*e, a, 2]
share|improve this answer
Thanks, however the second one only returns a^2 instead of b*c ... Maybe the best thing I could do is taking derivatives and evaluate at 0 ... – pablo Jan 25 '13 at 16:10
@pablo, please see my update. I maybe misinterpreted your question and hopefully gave now some more insight. – halirutan Jan 27 '13 at 11:32

I think this is what you want:

 a*k + (a^2)*b*c + b*e,
 term_ /; ! FreeQ[term, a],

{a^2 b c, a k}

Note that the terms are not in the explicit order given as Plus is Orderless. I am assuming this does not matter in practice but other measures can be taken of needed.

As a function:

selectTerms[expr_, pat_] := Cases[expr, term_ /; ! FreeQ[term, pat], {1}]
share|improve this answer


 Select[a*k + (a^2)*b*c + b*e, MemberQ[#, a | a^_] &]

=> a^2 b c + a k

Select[a*k + (a^2)*b*c + b*e, MemberQ[#, b] && FreeQ[#, a] &]

=> b e

Select[a*k + (a^2)*b*c + b*e, MemberQ[#, e] &]

=> b e

share|improve this answer

One rather obvious way, useful for the situation you gave, is as follows: You can evaluate the expression at e=0:

(a*k + (a^2)*b*c + b*e)/.e->0

gives a*k+a^2*b*c, and

(a*k + (a^2)*b*c + b*e)/.a->0

gives b*e.

share|improve this answer
You probably mean e -> 0 and a -> 0 instead of e=0 and a=0. – Karsten 7. Mar 6 '15 at 22:47
Although this solutions certainly works here, it may not in more general cases. Note that I have made the correction recommended by @Karsten7. – bbgodfrey Mar 6 '15 at 23:19
Thanks for the correction. And yes, this does not work in more general cases. – Tanmay Mar 27 '15 at 21:05

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.