根据匹配,正则表达式替换为单独替换

时间:2012-08-02 11:56:51

标签: c# regex replace pattern-matching

假设我有一个词典(及其替换词):

var replacements = new Dictionary<string,string>();
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]字'。

其中 Charsymbol 可以是@#$%^&amp; * [] ..以及单词有效后的']',即[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;
         });

我如何得到str,str1等比赛,即没有charsymbol的单词。

3 个答案:

答案 0 :(得分:1)

这适合吗?

(?<=[#@&$])(\w+)|[[\w+]]

它与您的示例中的以下内容匹配:

@ str是#str是[str]是&amp; str1 @ str2一个测试$ str2 < / p>

答案 1 :(得分:1)

试试这个Regex([@$&#\[])[a-zA-Z]*(\])?,并替换为string0

你的代码应该是这样的:

var replacements = new Dictionary<string, string>
                        {
                            {"str", "string0"},
                            {"str1", "string1"},
                            {"str2", "string2"}
                        };

String input="@str is a #str is a [str] is a &str @str can be done $str is @str";

foreach (var replacement in replacements)
{
    string pattern = String.Format(@"([@$&#\[]){0}(\])?", replacement.Key);
    var re = new Regex(pattern, RegexOptions.IgnoreCase | RegexOptions.Compiled);
    string output = re.Replace(input, 
                               String.Format("{0}", replacement.Value)); 
} 

答案 2 :(得分:1)

将正则表达式更改为:

// Move the * inside the brackets around [a-zA-Z]
// Change [a-zA-Z] to \w, to include digits.
string pattern = @"(([@$&#\[]+)(\w*)(\])*)";

更改此行:

replacements.TryGetValue(match.Value,out lookup);

到此:

replacements.TryGetValue(match.Groups[3].Value,out lookup);

注意:您的IgnoreCase不是必需的,因为您在正则表达式中匹配大写和小写。