简体   繁体   English

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

[英]Regex replace with separate replacement depending on the match

Suppose if I have a dictionary ( word and its replacements) as : 假设我有一个字典( 单词及其替换):

var replacements = new Dictionary<string,string>();
replacements.Add("str", "string0");
replacements.Add("str1", "string1");
replacements.Add("str2", "string2");
...

and an input string as : 和输入字符串:

 string input = @"@str is a #str is a [str1] is a &str1 @str2 one test $str2 also [str]";

Edit : 编辑
Expected output : 预期产量:

string0 is a string0 is string0 is string1 string2 one test string2

I want to replace all occurances of ' [CharSymbol]word ' with its corresponding entry/value from the dictionary. 我想用字典中相应的条目/值替换' [CharSymbol] word '的所有出现。

where Charsymbol can be @#$%^&*[] .. and also ']' after the word is valid ie [str] . Charsymbol可以是@#$%^&* [] ..以及单词有效后的']',即[str]。

I tried the following for replace 我尝试了以下替换

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

How do i get the match as str , str1 etc. ie word without charsymbol. 我如何得到str,str1等比赛,即没有charsymbol的单词。

Does this suit? 这适合吗?

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

It matches the following in your example: 它与您的示例中的以下内容匹配:

@ str is a # str is a [ str ] is a & str1 @ str2 one test $ str2 @ str是# str是[ str ]是一个& str1 @ str2一个测试$ str2

Try this Regex : ([@$&#\\[])[a-zA-Z]*(\\])? 试试这个正则Regex([@$&#\\[])[a-zA-Z]*(\\])? , and replace with string0 ,并用string0替换

your code should be like this: 你的代码应该是这样的:

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

Change your regex to this: 将你的正则表达式改为:

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

Change this line: 改变这一行:

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

to this: 对此:

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

Note: Your IgnoreCase isn't necessary, since you match both upper and lower case in the regex. 注意:您的IgnoreCase不是必需的,因为您在正则表达式中匹配大写和小写。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM