title: Regular Expression to replace begin and end of string
body: <p>Consider the following input:
  BEGINsomeotherstuffEND</p>
  
  <p>I'm trying to write a regular expression to replace BEGIN and END but only if they <strong>both</strong> exist.</p>
  
  <p>I got as far as:</p>
  
  <pre><code>(^BEGIN|END$)
  </code></pre>
  
  <p>Using the following c# code to then perform my string replacement:</p>
  
  <pre><code>private const string Pattern = "(^guid'|'$)";
  private static readonly Regex myRegex = new Regex(Pattern, RegexOptions.Compiled | RegexOptions.CultureInvariant | RegexOptions.ExplicitCapture | RegexOptions.Singleline | RegexOptions.IgnoreCase);
  var newValue = myRegex.Replace(input, string.empty);
  </code></pre>
  
  <p>But unfortunately that matches either of those - not only when they both exist.</p>
  
  <p>I also tried:</p>
  
  <pre><code>^BEGIN.+END$
  </code></pre>
  
  <p>But that captures the entire string and so the whole string will be replaced.</p>
  
  <p>That's about as far as my Regular Expression knowledge goes.</p>
  
  <p>Help!</p>