我需要能够匹配句子中的一系列单词并尝试使用正则表达式但由于某种原因,正则表达式将“not”和“no”评估为相等。代码仍处于模拟阶段但想法是系统抛出错误,我需要查看是否包含特定的措辞。这是代码。
string message = "The field could not be calculated because the following field(s) have no value or have invalid values: [field1].";
string[] sentences = message.Split(' ');
string pattern = "have no value or have invalid values:";
string[] pattern1 = pattern.Split(' ');
string[] result = new string[pattern1.Length];
int i = 0;
foreach (string p in pattern1)
{
foreach (string s in sentences)
{
System.Console.Write("{0,24}", s);
if (System.Text.RegularExpressions.Regex.IsMatch(s, p, System.Text.RegularExpressions.RegexOptions.IgnoreCase))
{
System.Console.WriteLine(" (match for '{0}' found)", p);
result[i] = s.Trim();
i++;
break;
}
else
{
System.Console.WriteLine();
}
}
}
bool isEqual = pattern1.SequenceEqual(result);
if (isEqual)
{
System.Console.WriteLine("Match Found");
}
else
{
System.Console.WriteLine("Match NOT Found");
}
答案 0 :(得分:2)
您的代码在这种情况下使用的正则表达式模式是no
(来自pattern1
,您可以通过拆分have no value or have invalid values:
获得
好not
肯定是正则表达式no
的匹配 - 因为not
以 no
开头。
如果您想更具体一点,可以介绍word boundary \b
//other code
foreach (string p in pattern1)
{
foreach (string s in sentences)
{
//wrap your pattern in word boundaries
string pat = "\b" + p + "\b"
//use the new wrapped pattern
if (System.Text.RegularExpressions.Regex.IsMatch(s, pat, System.Text.RegularExpressions.RegexOptions.IgnoreCase))
{
System.Console.WriteLine(" (match for '{0}' found)", pat);
}
else
{
//...
}
}
}