title: Escaping strings for gsub
body: <p>I read a file:</p>
  
  <pre><code>local logfile = io.open("log.txt", "r")
  data = logfile:read("*a")
  print(data)
  </code></pre>
  
  <p>output:</p>
  
  <pre><code>...
  "(\.)\n(\w)", r"\1 \2"
  "\n[^\t]", "", x, re.S
  ...
  </code></pre>
  
  <p>Yes, logfile looks awful as it's full of various commands</p>
  
  <p>How can I call <code>gsub</code> and remove i.e. <code>"(\.)\n(\w)", r"\1 \2"</code> line from data variable?</p>
  
  <p>Below snippet, does not work:</p>
  
  <pre><code>s='"(\.)\n(\w)", r"\1 \2"'
  data=data:gsub(s, '')
  </code></pre>
  
  <p>I guess some escaping needs to be done. Any easy solution?</p>
  
  <hr>
  
  <p><strong>Update</strong>:</p>
  
  <pre><code>local data = [["(\.)\n(\w)", r"\1 \2"
  "\n[^\t]", "", x, re.S]]
  
  local s = [["(\.)\n(\w)", r"\1 \2"]]
  
  local function esc(x)
     return (x:gsub('%%', '%%%%')
              :gsub('^%^', '%%^')
              :gsub('%$$', '%%$')
              :gsub('%(', '%%(')
              :gsub('%)', '%%)')
              :gsub('%.', '%%.')
              :gsub('%[', '%%[')
              :gsub('%]', '%%]')
              :gsub('%*', '%%*')
              :gsub('%+', '%%+')
              :gsub('%-', '%%-')
              :gsub('%?', '%%?'))
  end
  
  print(data:gsub(esc(s), ''))
  </code></pre>
  
  <p>This seems to works fine, only that I need to escape, escape character <code>%</code>, as it wont work if <code>%</code> is in matched string. I tried <code>:gsub('%%', '%%%%')</code> or <code>:gsub('\%', '\%\%')</code> but it doesn't work.</p>
  
  <hr>
  
  <p><strong>Update 2</strong>:</p>
  
  <p>OK, <code>%</code> can be escaped this way if set first in above "table" which I just corrected</p>
  
  <p>:terrible experience:</p>
  
  <p><strong>Update 3</strong>:</p>
  
  <p><em>Escaping of <code>^</code> and <code>$</code></em></p>
  
  <p>As stated in Lua manual (<a href="http://www.lua.org/manual/5.1/manual.html#5.4.1" rel="noreferrer" title="5.1">5.1</a>, <a href="http://www.lua.org/manual/5.2/manual.html#6.4.1" rel="noreferrer" title="5.2">5.2</a>, <a href="http://www.lua.org/manual/5.3/manual.html#6.4.1" rel="noreferrer" title="5.3">5.3</a>)</p>
  
  <blockquote>
    <p>A caret <code>^</code> at the beginning of a pattern anchors the match at the beginning of the subject string. A <code>$</code> at the end of a pattern anchors the match at the end of the subject string. At other positions, <code>^</code> and <code>$</code> have no special meaning and represent themselves.</p>
  </blockquote>
  
  <p>So a better idea would be to escape <code>^</code> and <code>$</code> only when they are found (respectively) and the beginning or the end of the string.</p>
  
  <p><em>Lua 5.1 - 5.2+ incompatibilities</em></p>
  
  <blockquote>
    <p><code>string.gsub</code> now raises an error if the replacement string contains a <code>%</code> followed by a character other than the permitted <code>%</code> or digit.</p>
  </blockquote>
  
  <p>There is no need to double every <code>%</code> in the replacement string. See <a href="http://lua-users.org/wiki/LuaFiveTwo" rel="noreferrer" title="lua-users">lua-users</a>.</p>