The given key was not present in the dictionary Match Evaluator delegate.

huangapple go评论56阅读模式
英文:

The given key was not present in the dictionary Match Evaluator delegate

问题

I'm trying to replace a string with regex via Match Evaluator delegate. [dictionary method] when the string starts with: .- &*?. I get a error with the following details:

the given key was not present in the dictionary.

What can I do?

IDictionary<string, string> dict = new Dictionary<string, string>()
{
    { "-", "e" },
    { "?", "z'" },
};

string str1 = "-50";
var p = new Regex(String.Join("|", dict.Keys));
string str2 = p.Replace(str1, x => dict[x.Value]);
英文:

I'm trying to replace a string with regex via Match Evaluator delegate. [dictionary method] when the string starts with: .- &amp;*?. I get a error with the following details:

> the given key was not present in the dictionary.

What can I do?

IDictionary&lt;string, string&gt; dict = new Dictionary&lt;string, string&gt; ()
{
    { &quot;-&quot;, &quot;e&quot;  },
    { &quot;?&quot;, &quot;z&#39;&quot; },
};

string str1 = &quot;-50&quot;
var p = new Regex(String.Join(&quot;|&quot;, dict.Keys));
str2 = p.Replace(str1, x =&gt; dict[x.Value]);

答案1

得分: 1

你应该Escape具有正则表达式中特殊含义的符号(例如?):

using System.Linq;
using System.Text.RegularExpressions;

...

IDictionary&lt;string, string&gt; dict = new Dictionary&lt;string, string&gt;() {
  { &quot;-&quot;, &quot;e&quot;  },
  { &quot;?&quot;, &quot;z&#39;&quot; }, 
};

var pattern = string.Join(&quot;|&quot;, dict
  .Keys
  .OrderByDescending(key =&gt; key.Length)
  .Select(key =&gt; Regex.Escape(key)));

string str1 = &quot;-50&quot;;

string str2 = Regex.Replace(str1, pattern, match =&gt; dict[match.Value]);

有一个小技巧是使用.OrderByDescending(key =&gt; key.Length):如果我们有一个模式是另一个模式的子字符串,比如

{&quot;--&quot;, &quot;x2&quot;},
{&quot;-&quot;, &quot;x1&quot;},  // &quot;-&quot;是&quot;--&quot;的子字符串

那么我们应该首先尝试更长的模式:--abc应该被转换为x2abc,而不是x1x1abc

英文:

You should Escape symbols (e.g. ?) which have special meaning in regular expressions:

using System.Linq;
using System.Text.RegularExpressions;

...

IDictionary&lt;string, string&gt; dict = new Dictionary&lt;string, string&gt;() {
  { &quot;-&quot;, &quot;e&quot;  },
  { &quot;?&quot;, &quot;z&#39;&quot; }, 
};

var pattern = string.Join(&quot;|&quot;, dict
  .Keys
  .OrderByDescending(key =&gt; key.Length)
  .Select(key =&gt; Regex.Escape(key)));

string str1 = &quot;-50&quot;;

string str2 = Regex.Replace(str1, pattern, match =&gt; dict[match.Value]);

There is a little trick with .OrderByDescending(key =&gt; key.Length): if we have pattern which is a substring of another one, say

{&quot;--&quot;, &quot;x2&quot;},
{&quot;-&quot;, &quot;x1&quot;},  // &quot;-&quot; is a substring of &quot;--&quot;

then we should try longer pattern first: --abc should be transformed into x2abc, not x1x1abc

huangapple
  • 本文由 发表于 2023年2月10日 06:15:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/75405019.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定