title: Regular expressions negative lookahead
body: <p>I'm doing some regular expression gymnastics. I set myself the task of trying to search for C# code where there is a usage of the as-operator not followed by a null-check within a reasonable amount of space. Now I don't want to parse the C# code. E.g. I want to capture code snippets such as</p>
  
  <pre><code>    var x1 = x as SimpleRes;
      var y1 = y as SimpleRes;
      if(x1.a == y1.a)
  </code></pre>
  
  <p>however, not capture</p>
  
  <pre><code>    var x1 = x as SimpleRes;
      var y1 = y as SimpleRes;
      if(x1 == null)
  </code></pre>
  
  <p>nor for that matter</p>
  
  <pre><code>    var x1 = x as SimpleRes;
      var y1 = y as SimpleRes;
      if(somethingunrelated == null) {...}
      if(x1.a == y1.a)
  </code></pre>
  
  <p>Thus any random null-check will count as a "good check" and hence not found. </p>
  
  <p><strong>The question is:</strong> How do I match something while ensuring something else is not found in its sourroundings.</p>
  
  <p>I've tried the naive approach, looking for 'as' then doing a negative lookahead within a 150 characters. </p>
  
  <pre><code>\bas\b.{1,150}(?!\b==\s*null\b)
  </code></pre>
  
  <p>The above regular expression matches all of the above examples infortunately. My gut tells me, the problem is that the looking ahead and then doing negative lookahead can find many situations where the lookahead does not find the '== null'. </p>
  
  <p>If I try negating the whole expression, then that doesn't help either, at that would match most C# code around.</p>