Код: Выделить всё
var replacements = new Dictionary();
replacements.Add("str", "string0");
replacements.Add("str1", "string1");
replacements.Add("str2", "string2");
...
Код: Выделить всё
string input = @"@str is a #str is a [str1] is a &str1 @str2 one test $str2 also [str]";
Ожидаемый результат:
Код: Выделить всё
string0 is a string0 is string0 is string1 string2 one test string2
где Charsymbol может быть @#$%^&*[] .. а также ']' после того, как слово допустимо, т.е. [str] .
Для замены я попробовал следующее
Код: Выделить всё
string input = @"@str is a #str is a [str1] is a &str1 @str2 one test $str2 also [str]";
string pattern = @"(([@$&#\[]+)([a-zA-Z])*(\])*)"; // correct?
Regex re = new Regex(pattern, RegexOptions.IgnoreCase | RegexOptions.Compiled);
// string outputString = re.Replace(input,"string0");
string newString = re.Replace(input, match =>
{
Debug.WriteLine(match.ToString()); // match is [str] #str
string lookup = "~"; // default value
replacements.TryGetValue(match.Value,out lookup);
return lookup;
});
Подробнее здесь: https://stackoverflow.com/questions/117 ... -the-match
Мобильная версия