匹配包含未知文本

时间:2017-11-27 22:47:20

标签: c# regex string contains

我有一个我正在调用的Web服务,它返回一个错误消息列表。然后我在这个列表上做一个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" />

1 个答案:

答案 0 :(得分:1)

如果你不喜欢像我一样采用正则表达式方法,你可以将已知的错误消息添加到HashSet中并将它们保存在内存中,然后查找哪个错误消息与手头的错误消息最匹配,喜欢比赛得分。

相关问题