title: How to remove only certain substrings from a string?
body: <p>Using C#, I have a string that is a SQL script containing multiple queries. I want to remove sections of the string that are enclosed in single quotes. I can do this using <code>Regex.Replace</code>, in this manner:</p>
  
  <pre><code>string test = "Only 'together' can we turn him to the 'dark side' of the Force";
  test = Regex.Replace(test, "'[^']*'", string.Empty);
  </code></pre>
  
  <p><strong>Results in</strong>: "Only  can we turn him to the  of the Force"</p>
  
  <p>What I want to do is remove the substrings between quotes EXCEPT for substrings containing a specific substring. For example, using the string above, I want to remove the quoted substrings except for those that contain "dark," such that the resulting string is:</p>
  
  <p><strong>Results in</strong>: "Only  can we turn him to the 'dark side' of the Force"</p>
  
  <p>How can this be accomplished using <code>Regex.Replace</code>, or perhaps by some other technique? I'm currently trying a solution that involves using <code>Substring()</code>, <code>IndexOf()</code>, and <code>Contains()</code>. </p>
  
  <p>Note: I don't care if the single quotes around "dark side" are removed or not, so the result could also be: "Only can we turn him to the dark side of the Force." I say this because a solution using <code>Split()</code> would remove all the single quotes.</p>
  
  <p><strong>Edit:</strong> I don't have a solution yet using <code>Substring()</code>, <code>IndexOf()</code>, etc. By "working on," I mean I'm thinking in my head how this can be done. I have no code, which is why I haven't posted any yet. Thanks.</p>
  
  <p><strong>Edit:</strong> VKS's solution below works. I wasn't escaping the \b the first attempt which is why it failed. Also, it didn't work unless I included the single quotes around the whole string as well.</p>
  
  <pre><code>test = Regex.Replace(test, "'(?![^']*\\bdark\\b)[^']*'", string.Empty);
  </code></pre>