title: Regular expression to match and replace "${foo}" but not "<c:if test="${foo}" />" in Java
body: <p>While anyone will explore <a href="https://stackoverflow.com/questions/17047789/how-to-search-for-foo-and-replace-by-cout-value-foo-in-eclipse-edito/17048652#17048652">this question</a>, the obvious question arises that, is there any regular expression possible that matches</p>
  
  <pre><code>${foo}
  </code></pre>
  
  <p>but not</p>
  
  <pre><code>&lt;c:if test="${foo}" /&gt;
  </code></pre>
  
  <p>or</p>
  
  <pre><code>&lt;c:when test="${foo}" /&gt;
  </code></pre>
  
  <p>to replace those stand alone <code>${foo}</code> with <code>&lt;c:out value="${foo}" /&gt;</code>, here <code>foo</code> can be any string as long as <code>${foo}</code> is a valid <a href="http://www.tutorialspoint.com/jsp/jsp_expression_language.htm" rel="nofollow noreferrer">el expression</a> in <code>jsp</code>.</p>
  
  <p><em>Edit:</em></p>
  
  <p><code>${foo}</code> can be anywhere as long as its not violating <code>jsp</code> syntax after the replacement! This is just a precaution to prevent <a href="https://stackoverflow.com/questions/4465583/alternative-to-using-cout-to-prevent-xss">XXS attack</a> in <code>jsp</code>'s.</p>