title: Regex negative lookbehind AND positive lookahead
body: <p>I'm trying to parse an <code>elasticsearch</code> query string and remove all invalid <code>*</code> characters using <code>negative lookbehind</code> and <code>negative lookahead</code>. </p>
  
  <p>For example, the following are legitimate uses:</p>
  
  <pre><code>[10 to *]
  {* TO 2012-01-01}
  {     * TO 2012-01-01}
  steakhou*
  </code></pre>
  
  <p>These are not legitimate:</p>
  
  <pre><code>10 to *  (no brackets)
  [10 to * (no proceeding bracket)
  *ab      (leading *)
  *        (standalone)
  </code></pre>
  
  <p>The expression I've come up with works for the most part except I can't figure out how to also <code>lookahead</code> and check if there is no closing <code>},)</code> or <code>]</code></p>
  
  <pre><code>(?&lt;!(?i)TO\s|\w|\[|\[\s|\]|\{|\()\*
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/Q1DCN.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Q1DCN.png" alt="enter image description here"></a> as you can see the <code>*</code> near the beginning of the sample text isn't matched</p>
  
  <p><strong>Edit:</strong> link for regex tester that allows lookahead/lookbehind
  <a href="https://regex101.com/" rel="nofollow noreferrer">https://regex101.com/</a></p>
  
  <p>sample text: </p>
  
  <pre><code>[1 to 5] 2 TO * -sample *0-9AZc *testing * 2344 +field value some_field mmm e-A[0-9]* [ *p{L} [n-z]] broken*
  awef * sdf TZ* [* i sto 5] {* to 3} 
  </code></pre>