在字典中使用 RegEx
Using RegEx in Dictionary
我是 C# 的新手,看起来我需要将 Regex
与 Dictionary<string, Action>
一起使用
下面我的工作示例,作为理解 C# 中的 Regex
的测试:
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
public class Program
{
public static void Main()
{
string sPattern = "word1|word2";
string input = "word2";
Match result = Regex.Match(input, sPattern);
Console.WriteLine(result);
}
}
我尝试将其包含在 Dictionary
中,如下所示,但失败了:
var functions = new Dictionary<Match, Action>();
functions.Add(Regex.Match(string, sPattern), CountParameters);
Action action;
if (functions.TryGetValue("word1|word2", out action)) {action.Invoke(); }
它给了我 invalid expression string at Regex.Match(string, sPattern)
和 cannot convert string to Match at .TryGetValue("word1|word2")
更新
我像下面这样重构了我的代码,所以我没有编译错误,但结果没有打印出来:
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
public class Program
{
public static void Main()
{
string sPattern1 = "word1|word2";
string sPattern2 = "word3|word4";
string input = "word2";
var functions = new Dictionary<string, Action>();
functions.Add("word1", CountParameters);
functions.Add("word3", SomeOtherMethodName);
Action action;
if (functions.TryGetValue((Regex.Match(input, sPattern1)).ToString(), out action))
{
action.Invoke();
}
else
{
// No function with that name
}
}
public static void CountParameters()
{
Console.WriteLine("Fn 1");
}
public static void SomeOtherMethodName()
{
Console.WriteLine("Fn 2");
}
}
如果字符串输入 = "word1",上面的方法是有效的;但如果字符串输入 = "word2" 则不起作用;而 RegEx 应该根据 string sPattern = "word1|word2";
将 word1 和 word2 视为相同
更新 2
如果不够清楚,上面的输出应该是:
如果输入是 word1 或 word2,则执行 CountParameters
,因为考虑到上面模式中使用的 |
,RegEx 应该认为它们相同。
如果输入是 word3 或 word4,则执行 SomeOtherMethodName
,因为考虑到上面模式中使用的 |
,RegEx 应该认为它们相同。
等等,以防我使用 OR
添加更多 RegEx 表达式,即 |
我想你想要这样的东西:
var input = "word2";
var functions = new Dictionary<Regex, Action>
{
{new Regex("word1|word2"), CountParameters}
};
functions.FirstOrDefault(f => f.Key.IsMatch(input)).Value?.Invoke();
我是 C# 的新手,看起来我需要将 Regex
与 Dictionary<string, Action>
下面我的工作示例,作为理解 C# 中的 Regex
的测试:
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
public class Program
{
public static void Main()
{
string sPattern = "word1|word2";
string input = "word2";
Match result = Regex.Match(input, sPattern);
Console.WriteLine(result);
}
}
我尝试将其包含在 Dictionary
中,如下所示,但失败了:
var functions = new Dictionary<Match, Action>();
functions.Add(Regex.Match(string, sPattern), CountParameters);
Action action;
if (functions.TryGetValue("word1|word2", out action)) {action.Invoke(); }
它给了我 invalid expression string at Regex.Match(string, sPattern)
和 cannot convert string to Match at .TryGetValue("word1|word2")
更新
我像下面这样重构了我的代码,所以我没有编译错误,但结果没有打印出来:
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
public class Program
{
public static void Main()
{
string sPattern1 = "word1|word2";
string sPattern2 = "word3|word4";
string input = "word2";
var functions = new Dictionary<string, Action>();
functions.Add("word1", CountParameters);
functions.Add("word3", SomeOtherMethodName);
Action action;
if (functions.TryGetValue((Regex.Match(input, sPattern1)).ToString(), out action))
{
action.Invoke();
}
else
{
// No function with that name
}
}
public static void CountParameters()
{
Console.WriteLine("Fn 1");
}
public static void SomeOtherMethodName()
{
Console.WriteLine("Fn 2");
}
}
如果字符串输入 = "word1",上面的方法是有效的;但如果字符串输入 = "word2" 则不起作用;而 RegEx 应该根据 string sPattern = "word1|word2";
更新 2
如果不够清楚,上面的输出应该是:
如果输入是 word1 或 word2,则执行
CountParameters
,因为考虑到上面模式中使用的|
,RegEx 应该认为它们相同。如果输入是 word3 或 word4,则执行
SomeOtherMethodName
,因为考虑到上面模式中使用的|
,RegEx 应该认为它们相同。
等等,以防我使用 OR
添加更多 RegEx 表达式,即 |
我想你想要这样的东西:
var input = "word2";
var functions = new Dictionary<Regex, Action>
{
{new Regex("word1|word2"), CountParameters}
};
functions.FirstOrDefault(f => f.Key.IsMatch(input)).Value?.Invoke();