title: Regex to replace only if not preceded by
body: <p>Apologies for my blatant lack of knowledge on regular expressions, I understand lots of questions crop up here on them, but try as I might for hours on end I cannot figure this out.
  Basically what I am trying to do is replace all occurrences of 6 digit numbers (with or without hyphens) in a string. However I do not wish to replace numbers if preceded by certain words.</p>
  
  <p>This <a href="https://stackoverflow.com/questions/406230/regular-expression-to-match-string-not-containing-a-word">Regular expression to match a line that doesn&#39;t contain a word?</a> solution comes close to what I am looking for but I cannot seem to use it in a way that works for my requirements.</p>
  
  <p>What I need is as follows:
  For the string:</p>
  
  <p>"User paid £43 on 23/05/14 to account 123456 with cheque 123456 transaction: 123456."</p>
  
  <p>I wish to only replace the 6 digit number not preceded by "cheque", or "transaction:". What I have been trying is as follows:</p>
  
  <pre><code>\b[0-9]{2}-?[0-9]{2}-?[0-9]{2}\b 
  </code></pre>
  
  <p>(This replaces all 6 digit numbers)</p>
  
  <p>Using this <a href="https://stackoverflow.com/questions/14344129/how-do-you-replace-a-match-using-regex-only-if-it-is-not-preceded-by-a-given-c">How do you replace a match, using regex, only if it is not preceded by a given character?</a> answer, I tried </p>
  
  <pre><code>(^cheque\s[0-9]{2}-?[0-9]{2}-?[0-9]{2}\b) 
  </code></pre>
  
  <p>(Please note I am trying first for one of the words I wish to escape and will then include the others.)
  This does not replace any of the 6 digit numbers.</p>
  
  <p>Through trial and error I have found </p>
  
  <pre><code>(cheque\s+[0-9]{2}-?[0-9]{2}-?[0-9]{2}\b) 
  </code></pre>
  
  <p>will replace the word cheque followed by a 6 digit number so I am getting there - but I need to negate this (and transaction followed by 6 digit number) and replace instead the 6 digit number <em>not</em> preceded by these words.</p>
  
  <p>This <a href="https://stackoverflow.com/questions/2637675/how-to-negate-the-whole-regex">How to negate the whole regex?</a> answer is helpful on figuring out how to negate the expression but try as I might, I cannot find how to make it work for my situation. I tried</p>
  
  <pre><code>^(?!(?:((transaction\s+[0-9]{2}-?[0-9]{2}-?[0-9]{2}\b) )|((cheque\s+[0-9]{2}-?[0-9]{2}-?[0-9]{2}\b) ))$).*$
  </code></pre>
  
  <p>but this replaced the whole string!</p>
  
  <p>Any help on this is greatly appreciated.</p>
  
  <p>Thanking you.</p>