title: regular expression to rewrite 1x as x in mathematical expression
body: <p>I have texts that can contain mathematical expressions where 1x can occur, or more general, a 1 followed by a letter. What I'm trying to do is rewrite the math within the text with a regular expression so that all occurrences of 1x are replaced by x, but not so that ie. 21x becomes 2x. Also, the math within the text is within # delimiters and the 1x should only be replace when it is between the delimiters. </p>
  
  <p>An example text is: </p>
  
  <p>"As a 1st step, you can rewrite #21x*x - 1y# is equal to #1z + 1#"</p>
  
  <p>this must become</p>
  
  <p>"As a 1st step, you can rewrite #21x^2 - y# is equal to #z + 1#"</p>
  
  <p>What I have so far as a regular expression pattern is </p>
  
  <pre><code>'/(#.*?)(\D?)1([a-z])(.*?#)/i'
  </code></pre>
  
  <p>and as a replacement</p>
  
  <pre><code>'$1$2$3$4'
  </code></pre>
  
  <p>but it does not do the job.
  Any help on the pattern would be appreciated.</p>
  
  <p>Thanks</p>