title: Escape sed delimiter character when also regex special character
body: <p>Say I have want to match the string <code>foo.</code>. My regex would be <code>foo\.</code>, to escape the special meaning of the <code>.</code> character.</p>
  
  <p>Now say that in my sed command, I use the <code>.</code> as a delimiter.</p>
  
  <p>For a search like, say, <code>cat</code>, this works fine.</p>
  
  <pre><code>echo cat | sed 's.cat.dog.g'
  </code></pre>
  
  <p>But when I have a literal <code>.</code> in my search string, I run into a problem. I would think, that since the <code>.</code> is my delimiter, my regex <code>foo\.</code> should become <code>foo\\\.</code>. Ideally, the first pass of unescaping would be done by sed to make this into <code>foo\.</code>, and then this would get passed along to the regular expression engine.</p>
  
  <p>But instead, it matches <code>cat\\\.</code></p>
  
  <pre><code>echo 'cat.' | sed 's.cat\\\..dog.g' # cat.
  echo 'cat\.' | sed 's.cat\\\..dog.g' # dog
  </code></pre>
  
  <p>Is there any way to make this work with a literal <code>.</code> when <code>.</code> is used as a delimiter?</p>
  
  <p>I'm asking this question because I want to be able to have a function</p>
  
  <pre><code>function escapeForExtendedSed ($str, $delimiter = "/") { ... }
  </code></pre>
  
  <p>which can escape correctly depending on what the caller is using as a delimiter.</p>