title: How to search and replace in mathematica?
body: <p>I'm looking to learn how to search through a polynomial and replace all points P[x,y] that are contained within a list of finite points {P[a,b], P[c,d],..., P[x,y]} with a term such as (q+ab) and replace all points, P[a,b] that are not contained in the list with a different term, (w+cd), for example. </p>
  
  <p>Here's  some code that I've been trying...</p>
  
  <p><code>K[poly_, pairs_] := 
   poly //. IF[MemberQ[pairs, P[a_, b_]], P[a_, b_] :&gt; (q+xy), 
     P[a_, b_] :&gt; (w+cd)]</code></p>
  
  <p>where //. is to replace through all pairs, IF conditional to replace pairs with the appropriate term, and MemberQ to check whether a pair P[a,b] is within a given list, 'pairs'</p>
  
  <p>To verify any suggestions, the input </p>
  
  <pre><code>K[ -q P[1,3] P[4,6] , {P[1,3], P[2,7]}] 
  </code></pre>
  
  <p>should output</p>
  
  <pre><code>-ab cd q - cd q^2 - ab q w - q^2 w
  </code></pre>
  
  <p>Thank you in advance for any help!</p>