title: Regular expression to find non-encapsulated text that's also not commented out
body: <p>I'm working on migrating a database into a sql project, and need to replace all instances of cross-database calls with a SQLCMD variable, and am struggling to write a regex to help me find the places I still need to update.</p>
  
  <p>In the SQL, we have the following:</p>
  
  <pre><code>MyOtherDatabase.MySchema.MyTable
  [MyOtherDatabase].MySchema.MyTable
  </code></pre>
  
  <p>Which I need to change to:</p>
  
  <pre><code>[$(MyOtherDatabase)].MySchema.MyTable
  </code></pre>
  
  <p>So far, I've come up with the following regex:</p>
  
  <pre><code>([^(]M|^M)yOtherDatabase
  </code></pre>
  
  <p>Which finds all places where "MyOtherDatabase" is used, and hasn't been replaced with the variable.</p>
  
  <p>HOWEVER, it's also picking it up in SQL comments, such as:</p>
  
  <pre><code>-- I don't want to find MyOtherDatabase in this line
  </code></pre>
  
  <p>and</p>
  
  <pre><code>FROM ADifferentPlace -- Used to be MyOtherDatabase
  </code></pre>
  
  <p>If this was only a few instances, I'd live with it, but I've currently got 560 matches, most of which are one or the other of the above, making it very easy for human error to get in the way.</p>
  
  <p>I'm using this regex in the "Search" box within Visual Studio 2015, with the "use regex" checkbox ticked.</p>
  
  <p>any advice would be helpful!</p>
  
  <p><strong>Edit</strong></p>
  
  <p>Also need to NOT find the following:</p>
  
  <pre><code>from MyTable -- from MyOtherDatabase.MySchema.MyTable
  </code></pre>