【问题标题】:Regex replace with separate replacement depending on the match正则表达式根据匹配替换为单独的替换
【发布时间】:2012-08-02 11:56:51
【问题描述】:

假设我有一本字典(word 及其替代品):

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]word”。

其中 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;
         });

我如何获得匹配 str , str1 等,即没有字符符号的单词。

【问题讨论】:

    标签: c# regex replace pattern-matching


    【解决方案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 不是必需的,因为您匹配正则表达式中的大小写。

    【讨论】:

    • nope..now 它给出“string0 is a string0 is a str1] is a str2 str1 can be done string0 is str2”
    • 我错过了 [a-zA-Z] 不匹配数字的事实。我已经更新了答案。
    【解决方案2】:

    这适合吗?

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

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

    @str是一个#str是一个[str]是一个&str1@str2一个测试$str2

    【讨论】:

    • 不抱歉没有按预期工作。我已经更新了问题以显示我是如何匹配/替换的。
    【解决方案3】:

    试试这个Regex([@$&amp;#\[])[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)); 
    } 
    

    【讨论】:

    • sorry 不适用于 question.ie 中的输入字符串。 "@str is a #str is a [str1] is a &str1 @str2 one test $str2 also [str]"
    猜你喜欢
    • 1970-01-01
    • 2012-05-16
    • 2012-09-28
    • 2013-12-06
    • 2021-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-30
    相关资源
    最近更新 更多