Skip to content

Instantly share code, notes, and snippets.

@hagbarddenstore
Created June 2, 2014 15:41
Show Gist options
  • Save hagbarddenstore/05835f266c56cc93de3c to your computer and use it in GitHub Desktop.
Save hagbarddenstore/05835f266c56cc93de3c to your computer and use it in GitHub Desktop.
public static string Replace(string input, string regex)
{
var match = System.Text.RegularExpressions.Regex.Match(regex, @"^s(.)(?<Regex>.*?)(?:(?<!\\)\1)(?<Replace>.*?)(?:(?<!\\)\1)$");
var replaced = Regex.Replace(input, match.Groups["Regex"].Value, match.Groups["Replace"].Value);
return replaced;
}
var replaced1 = Replace("value", "s/value/VALUE/");
Console.WriteLine(replaced1); // VALUE
var replaced2 = Replace("value/value", "s/value\\/value/VALUE/");
Console.WriteLine(replaced2); // VALUE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment