title: Regex to replace part of a query string with literals
body: <p>I have a slight problem in that I am passing several SQL Server <code>Like</code> queries (which I don't have control over) in my .Net application and I need to escape some of the characters to make them literals for the database.</p>
  
  <p>For example, suppose the field I am trying to query looks something like:</p>
  
  <p><code>This_is [a] long_text field with [literal] characters in it</code></p>
  
  <p>And my query looks something like this:</p>
  
  <p><code>SELECT * FROM [MyTable] where [MyField] Like 'This_% [literal]%'</code></p>
  
  <p>Now, this <strong>should</strong> get that data, the challenge is that the <code>_</code> and <code>[</code> characters are not seen by SQL Server as the literal characters, so I have to update my query to:</p>
  
  <p><code>SELECT * FROM [MyTable] where [MyField] Like 'This[_]% [[]literal]%'</code></p>
  
  <p>And then it works.</p>
  
  <hr>
  
  <p>So, my question is that I would like to write a VB.Net function that uses RegEx to find the <code>Like</code> part of the statement and then to replace the characters.</p>
  
  <p>Something along the lines of:</p>
  
  <pre><code>Public Function ConvertQuery(strSQL as string)
  
  ... Some kind of Regex that searches for the word "Like" 
      and then takes everytihng between the 2 "'" characters
      Then replaces the "_" with "[_]" and "[" with "[[]"
  
  Return ChangedString
  End Function
  </code></pre>
  
  <p>I know I am giving REALLY bad details, I know what i want, I just can't figure out how to do it with RegEx.</p>
  
  <p>ALSO, if there is a smarter way to do this with SQL Server <code>Like</code> statments, <strong>please</strong> also let me know!!!</p>
  
  <p>Thanks!!</p>