0

我是 c#(Pattern) 中的验证字符串。

与验证有关的是,它包含一个或多个词动态

例如 :

第一个字符串 -工单更新 - ID:新 ,优先级: 在 GSD 中为您分配了一个新工单/操作项目。

第二个字符串 - # Ticket Update- ID: #  with Priority: # 在 GSD 中为您分配了一个新的工单/操作项目。

我的数据库中有第二个字符串,我用#替换了动态词,它可以是任何东西。

如果第一个字符串与第二个字符串中给出的模式匹配,则第一个字符串将用于验证。

我知道它可以使用字符串拆分操作来完成,但是我们是否有任何替代方法可以提高效率,因为拆分操作很繁重,就像我们可以使用正则表达式或其他东西一样。

IF 第一个字符串是:AnyWordWithNumbers 工单更新 ID:AnyWordWithNumbers 优先级:AnyWordWithNumbers 在 GSD 中为您分配了一个新工单/操作项。

所以这个字符串是有效的.. IF 第一个字符串是:AnyWordWithNumbers Tt 更新 ID:AnyWordWithNumbers 优先级:AnyWordWithNumbers 在 GSD 中为您分配了一个新的工单/行动项目

最后一个 (.) 缺少哪个并且票的拼写不正确是无效的。

Not : 用粗体标记可以是任何东西

4

3 回答 3

1

您可以使用此正则表达式:

private static readonly Regex TestRegex = new Regex(@"^([A-Za-z0-9]+) Ticket Update- ID:\1 with Priority:\1 A New ticket/action item is assigned to you in GSD\.$");

public bool IsValid(string testString)
{ 
   return (TestRegex.IsMatch(testString));
}
于 2013-06-06T09:09:09.593 回答
0

在 Regex 中,\w代表字母、下划线和数字。因此,您可以使用以下方法进行测试:

If (Regex.IsMatch(input, @"^\w+ Ticket Update- ID:\w+ with Priority:\w+ A New ticket/action item is assigned to you in GSD\.$") {
}

如果它必须是同一个词三次,您可以按其编号使用较早的匹配组\n

If (Regex.IsMatch(input, @"^(\w+) Ticket Update- ID:\1 with Priority:\1 A New ticket/action item is assigned to you in GSD\.$") {
}

如果你想允许不同的空格,你可以使用\s*which 代表任意数量的空格或\s+至少一个空格:

If (Regex.IsMatch(input, @"^\s*\w+\s+Ticket Update- ID\s*:\s*\w+\s+with Priority\s*:\s*\w+\s+A New ticket/action item is assigned to you in GSD\.$") {
}
于 2013-06-06T09:01:16.943 回答
0

以下方法将为您提供预期的结果。

static bool IsMatch(string templateString, string input)
{
    string[] splittedStr = templateString.Split('#');
    string regExString = splittedStr[0];
    for (int i = 1; i < splittedStr.Length; i++)
    {
        regExString += "[\\w\\s]*" + splittedStr[i];
    }

    Regex regEx = new Regex(regExString);
    return regEx.IsMatch(input);
}

使用上述方法如下,

string templateString = "# Ticket Update- ID:# with Priority:# A New ticket/action item is assigned to you in GSD";
string testString = "New Ticket Update- ID:New with Priority:New A New ticket/action item is assigned to you in GSD";
bool test = IsMatch(templateString, testString);
于 2013-06-06T09:18:29.813 回答