匹配包含未知文本的字符串
Match string that contain unknown text
我有一个 Web 服务,我称之为 returns 错误消息列表。然后我在这个列表上做一个 foreach,并根据配置文件中的错误消息文本进行匹配。但是,从 Web 服务返回的某些错误消息包含一些未知数据,例如日期或数字。
如何使用 C# 匹配此文本?我是否必须拆分字符串并尝试匹配每个单词?在执行“.Contains(...)”时如何处理日期或数字等未知变量?
这是一个例子:
Web 服务列表可能包含以下内容
"This is an example static error message"
"Another example static error message"
"This is an error message for employee 2"
"This is an error message dated 11/2/2017"
"Employee 3 does not work here anymore"
在我的配置文件中,我有以下内容:
<add errorText="This is an example static error message" field="N/A" />
<add errorText="Another example static error message" field="N/A" />
<add errorText="This is another example for employee **X**" field="N/A" />
<add errorText="This is an error message dated **X**" field="N/A" />
<add errorText="Employee **X** does not work here anymore" field="N/A" />
你可以用Regex
来匹配他们:
Regex.IsMatch(message, "This is another example for employee .+")
Regex.IsMatch(message, "This is an error message dated .+")
根据您的配置文件,您可以按如下方式构建正则表达式:
String configString = GetConfigString(3); // "This is another example for employee **X**"
String regexPattern = String.Concat("^", configString.Replace("**X**", ".+"), "$");
Boolean match = Regex.IsMatch("This is another example for employee John", regexPattern);
然后使用这样的正则表达式来匹配您的文本字符串。
您还可以在应用程序启动后立即构建所有正则表达式模式,并将它们缓存在某处以供将来使用:
String configStrings = GetConfigStrings();
String[] regexPatterns = new String[configStrings.Length];
for (Int32 i = 0; i < configStrings.Length; ++i)
regexPatterns[i] = String.Concat("^", configStrings[i].Replace("**X**", ".+"), "$");
由于您的框架中有多种可能的字符串替换类型,因此坚持使用 .+
标记是更好的选择。
当然,最终构建配置文件解析器并实施 GetConfigString
和 GetConfigStrings
方法(或仅实施一个,取决于您要使用的方法)取决于您。
如果你像我一样不喜欢使用正则表达式,你可以将已知的错误消息添加到一个 HashSet 中并保存在内存中,然后查找与手头的错误消息最匹配的错误消息,就像比赛比分。
我有一个 Web 服务,我称之为 returns 错误消息列表。然后我在这个列表上做一个 foreach,并根据配置文件中的错误消息文本进行匹配。但是,从 Web 服务返回的某些错误消息包含一些未知数据,例如日期或数字。
如何使用 C# 匹配此文本?我是否必须拆分字符串并尝试匹配每个单词?在执行“.Contains(...)”时如何处理日期或数字等未知变量?
这是一个例子:
Web 服务列表可能包含以下内容
"This is an example static error message"
"Another example static error message"
"This is an error message for employee 2"
"This is an error message dated 11/2/2017"
"Employee 3 does not work here anymore"
在我的配置文件中,我有以下内容:
<add errorText="This is an example static error message" field="N/A" />
<add errorText="Another example static error message" field="N/A" />
<add errorText="This is another example for employee **X**" field="N/A" />
<add errorText="This is an error message dated **X**" field="N/A" />
<add errorText="Employee **X** does not work here anymore" field="N/A" />
你可以用Regex
来匹配他们:
Regex.IsMatch(message, "This is another example for employee .+")
Regex.IsMatch(message, "This is an error message dated .+")
根据您的配置文件,您可以按如下方式构建正则表达式:
String configString = GetConfigString(3); // "This is another example for employee **X**"
String regexPattern = String.Concat("^", configString.Replace("**X**", ".+"), "$");
Boolean match = Regex.IsMatch("This is another example for employee John", regexPattern);
然后使用这样的正则表达式来匹配您的文本字符串。
您还可以在应用程序启动后立即构建所有正则表达式模式,并将它们缓存在某处以供将来使用:
String configStrings = GetConfigStrings();
String[] regexPatterns = new String[configStrings.Length];
for (Int32 i = 0; i < configStrings.Length; ++i)
regexPatterns[i] = String.Concat("^", configStrings[i].Replace("**X**", ".+"), "$");
由于您的框架中有多种可能的字符串替换类型,因此坚持使用 .+
标记是更好的选择。
当然,最终构建配置文件解析器并实施 GetConfigString
和 GetConfigStrings
方法(或仅实施一个,取决于您要使用的方法)取决于您。
如果你像我一样不喜欢使用正则表达式,你可以将已知的错误消息添加到一个 HashSet 中并保存在内存中,然后查找与手头的错误消息最匹配的错误消息,就像比赛比分。