Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#RegEx替换字符串出现次数的字典_C#_Regex_Dictionary - Fatal编程技术网

C#RegEx替换字符串出现次数的字典

C#RegEx替换字符串出现次数的字典,c#,regex,dictionary,C#,Regex,Dictionary,我试图用C#中字典中的值替换属性名称 我有以下字典: Dictionary<string, string> properties = new Dictionary<string, string>() { { "property1", @"E:\" }, { "property2", @"$(property1)\Temp"}, { "property3", @"$(property2)\AnotherSubFolder"} }; 鉴于案文: $(

我试图用C#中字典中的值替换属性名称

我有以下字典:

Dictionary<string, string> properties = new Dictionary<string, string>()
{
    { "property1", @"E:\" },
    { "property2", @"$(property1)\Temp"},
    { "property3", @"$(property2)\AnotherSubFolder"}
};
鉴于案文:

$(property2)\AnotherSubFolder
它突出显示$(property2)

但是,将这些放在.NET fiddle中,我没有找到与以下代码匹配的代码:

var pattern = @"\$\(([^)]+)\)/g";
Console.WriteLine(Regex.Matches(@"$(property2)AnotherSubFolder", pattern).Count);
输出0

我不太清楚为什么会在这里。为什么我的比赛结果为零

  • 默认情况下,.NET应全局匹配
  • 我不知道对
    /g
    的支持,因为这是一个Perl ism,所以请删除它,领先的
    /
    ,.NET正试图从字面上匹配它们

  • 正则表达式在这里可能有些过火,如果您的属性或值包含特殊字符或将作为正则表达式本身计算的字符,甚至可能会带来问题

    一个简单的替换应该可以:

    Dictionary<string, string> properties = new Dictionary<string, string>()
    {
        { "property1", @"E:\" },
        { "property2", @"$(property1)\Temp"},
        { "property3", @"$(property2)\AnotherSubFolder"}
    };
    
    Dictionary<string, string> newproperties = new Dictionary<string, string>();
    
    // Iterate key value pairs in properties dictionary, evaluate values
    foreach ( KeyValuePair<string,string> kvp in properties ) {
      string value = kvp.Value;
      // Execute replacements on value until no replacements are found
      // (Replacement of $(property2) will result in value containing $(property1), must be evaluated again)
      bool complete = false;
      while (!complete) {
        complete = true;
        // Look for each replacement token in dictionary value, execute replacement if found
        foreach ( string key in properties.Keys ) {
          string token = "$(" + key + ")";
          if ( value.Contains( token ) ) {
            value = value.Replace( "$(" + key + ")", properties[key] );
            complete = false;
          }
        }
      }
      newproperties[kvp.Key] = value;
    }
    
    properties = newproperties;
    
    字典属性=新字典()
    {
    {“property1”,@“E:\”},
    {“property2”,@“$(property1)\Temp”},
    {“property3”,@“$(property2)\n其他子文件夹”}
    };
    Dictionary newproperties=newdictionary();
    //迭代属性字典中的键值对,计算值
    foreach(属性中的KeyValuePair kvp){
    字符串值=kvp.value;
    //对值执行替换,直到找不到替换
    //(替换$(property2)将导致值包含$(property1),必须重新计算)
    bool complete=false;
    而(!完成){
    完整=正确;
    //在字典值中查找每个替换标记,如果找到,则执行替换
    foreach(properties.Keys中的字符串键){
    字符串标记=“$”(“+key+”);
    if(value.Contains(令牌)){
    value=value。替换(“$(“+key+”),属性[key]);
    完整=错误;
    }
    }
    }
    newproperties[kvp.Key]=值;
    }
    属性=新属性;
    
    我非常确定您需要删除“/g”,因为.Net不将其视为开关,而是将其视为模式的一部分。此外,如果您要测试要在.Net中使用的正则表达式,您应该确保使用实际使用.Net的工具,如.Yep good man。“/g”和第一个“/”看起来像是有问题的模式字符。如果我把他们干掉,我现在就能得到比赛。把这个作为答案,我会接受的。对我来说是新的——但不幸的是,你必须根据它的外观付费。我从来没有付费过,只需点击“立即试用”按钮。或者只是google for“.Net正则表达式测试仪”有不止一个。实际上,它不是Perl特有的,但是Perl使它出名了。谢谢布兰登。这可能有点过分,但现在已经完成了。让我们看看进展如何。
    Dictionary<string, string> properties = new Dictionary<string, string>()
    {
        { "property1", @"E:\" },
        { "property2", @"$(property1)\Temp"},
        { "property3", @"$(property2)\AnotherSubFolder"}
    };
    
    Dictionary<string, string> newproperties = new Dictionary<string, string>();
    
    // Iterate key value pairs in properties dictionary, evaluate values
    foreach ( KeyValuePair<string,string> kvp in properties ) {
      string value = kvp.Value;
      // Execute replacements on value until no replacements are found
      // (Replacement of $(property2) will result in value containing $(property1), must be evaluated again)
      bool complete = false;
      while (!complete) {
        complete = true;
        // Look for each replacement token in dictionary value, execute replacement if found
        foreach ( string key in properties.Keys ) {
          string token = "$(" + key + ")";
          if ( value.Contains( token ) ) {
            value = value.Replace( "$(" + key + ")", properties[key] );
            complete = false;
          }
        }
      }
      newproperties[kvp.Key] = value;
    }
    
    properties = newproperties;