// test using System; using GregoryAdam.Base.ExtensionMethods; namespace BaseTest { class test3 { //______________________________________________________________________ static void Main() { string[,] test = { { "hello", "o", "123", "hell123"}, { "+1+2", "+", "-", "-1-2" }, { "$1$2", "$", "-", "-1-2" }, { "{1+{2", "{", "-", "-1+-2" }, { "{1+{2", "{", "$", "$1+$2" }, { "hello", "O", "$$", "hell$$"}, { "hello", "H", @"$1\$2", @"$1\$2ello"}, { "hello", "H", null, @"ello"} }; for (int i = 0; i <= test.GetUpperBound(0); i++) { string input = test[i, 0]; string from = test[i, 1]; string to = test[i, 2]; string expect = test[i, 3]; string got = input.Replace(from, to, true); Console.WriteLine("input: {0}, expect: {1}, got: {2}, result: {3}", input, expect, got, expect == got); } Console.ReadLine(); } } }
using System; using System.Text; using System.Text.RegularExpressions; using GregoryAdam.Base.Constants; namespace GregoryAdam.Base.ExtensionMethods { public static partial class ExtensionMethods_String { //______________________________________________________________________ /// <summary> /// Replaces every occurrence of oldValue with newValue using Regex /// </summary> /// <param name="s"></param> /// <param name="oldValue">The string to be replaced</param> /// <param name="newValue">The string that replaces all occurrences of oldValue /// <para>May be null or the empty string</para></param> /// </param> /// <param name="ignoreCase"></param> /// <returns></returns> public static string Replace( this string s, string oldValue, string newValue, bool ignoreCase ) { if (string.IsNullOrEmpty(s) || string.IsNullOrEmpty(oldValue)) return s; // oldValuePattern string oldValuePattern = Regex.Replace( oldValue, RegexConstants.SpecialPatternCharsPattern, RegexConstants.SpecialPatternCharsPatternReplace ); string newValuePattern; if (string.IsNullOrEmpty(newValue)) newValuePattern = ""; else newValuePattern = Regex.Replace( newValue, RegexConstants.SpecialReplaceCharsPattern, RegexConstants.SpecialReplaceCharsPatternReplace ); RegexOptions regexOptions = !ignoreCase? RegexOptions.None : RegexOptions.CultureInvariant | RegexOptions.IgnoreCase; return Regex.Replace(s, oldValuePattern, newValuePattern, regexOptions); } //______________________________________________________________________ } }
using System.Text; namespace GregoryAdam.Base.Constants { public static class RegexConstants { //______________________________________________________________________ /// <summary> /// Characters that need to be escaped in a pattern /// </summary> public static readonly char[] SpecialPatternChars = { '\\', '^', '$', '.', '[', ']', '(', ')', '|', '{', '}', '*', '+', '?' }; //______________________________________________________________________ public static readonly string SpecialPatternCharsPattern = Build_SpecialPatternCharsPattern(SpecialPatternChars); //______________________________________________________________________ public static string SpecialPatternCharsPatternReplace = @"\$1"; //______________________________________________________________________ /// <summary> /// chars that have to be escaped with $ in the replacement pattern /// </summary> public static readonly char[] SpecialReplaceChars = { '$' }; //______________________________________________________________________ public static readonly string SpecialReplaceCharsPattern = Build_SpecialPatternCharsPattern(SpecialReplaceChars); //______________________________________________________________________ public static readonly string SpecialReplaceCharsPatternReplace = @"$$$1"; //______________________________________________________________________ private static string Build_SpecialPatternCharsPattern(char[] chars) { // a b c becomes // (\a|\b|\c) // length * 3 + 1 + 1 - 1 StringBuilder sb = new StringBuilder(chars.Length * 3 + 1); sb.Append('('); foreach (char c in chars) { sb.AppendFormat(@"\{0}|", c); } sb[sb.Length-1] = ')'; return sb.ToString(); } //______________________________________________________________________ } }