我正在尝试将表格中的模式与用户话语进行匹配。
string userUtterance = "I want identification number for number of customers";
string pattern1 = "identification number";
string pattern2 = "tom";
string pattern3 = "id";
期望的结果:
bool match1 = regex.Ismatch(userUtterance, pattern1); // should match
if(match1 == true)
{
// replace only the matched pattern in userUtterance
};
bool match2 = regex.Ismatch(userUtterance, pattern2); // should not match
bool match3 = regex.Ismatch(userUtterance, pattern3); // should not match
我想建议使用匹配该语法的正则表达式来限制模糊匹配并精确匹配文字。
由于
答案 0 :(得分:3)
您可以将锚 \b
用于字边界:
"\bonly these words\b"
这将在这些句子中仅匹配这些单词:
这里只有宝贝这几个字。
以下是“只有这些话”的宝贝。
这里只有这些词,宝贝。
这里只有这些词。
我说:'只有这些话'。
答案 1 :(得分:0)
替换正则表达式替换你可以试试这个:
string userUtterance = "I want identification number for number of customers";
string pattern1 = "identification number";
string pattern2 = "\btom \b";
string pattern3 = "\bid \b";
string replacement = "{YourWordHere}"
string newuserUtterance = userUtterance.Replace(pattern1, replacement );
bool match2 = Regex.IsMatch(newuserUtterance, pattern2); // should not match
bool match3 = Regex.IsMatch(newuserUtterance, pattern3); // should not match
这将使用pattern1
userUtterance
中的replacement
然后测试tom
< =注意空格,是否在新创建的字符串中,然后它将测试id
< =再次注意空格,是否在新字符串中。