# How can I exclude factors containing a certain symbol form matching my pattern

Ok, I give up. I can't figure how to tell Mathematica to do replacement using a pattern except for some special case. Easier to explain with small example.

I want to change pattern Exp[any_. c] to c for all cases of pattern any except when any happened to be contain the literal symbol x anywhere in it.

So Exp[5 c] will be replaced to c and so will Exp[y c] but for Exp[x c] or Exp[Sin[x] c] I want no replacement to happen since the term multiplying c has x in it.

I am trying to use Except but can't get it to work. This is what I tried

This does not work

ClearAll[x,c]
Exp[ x c] /. Exp[any_. c]:> c


So I need a way to set a pattern which says anything except x. So tried

Exp[ x  c] /.  Exp[Alternatives[Except[x], any1_.]  c] :> c


It changed it to c, so pattern matched.

Do I need to use Verbatim in pattern for this? How?

I reached my limit in pattern matching skills in Mathematica so I need help :)

• Maybe Exp[any_. c] /; FreeQ[any, x] :> c – Coolwater Jul 21 '17 at 8:30
• @Coolwater nice! It seems to work. Will test it more. Thanks. I was wondering how Except[] can be used there. I saw Except[] used in Cases but not in Replace in the help pages. – Nasser Jul 21 '17 at 8:39
• Since you want to test for the presence of x anywhere in the prefactor to c, I don't thing Except is going to be of much use here. I can't think of a pattern that would match any expression that contains an x somewhere without using FreeQ or MemberQ as conditions. The problem here is that you're trying to find x at any level of the expression, so you'll have to use FreeQ or MemberQ in some way. – Sjoerd Smit Jul 21 '17 at 10:35

ReplaceAll[