精确/文字单词或模式匹配正则表达式

kas*_*f4u 2 c# regex wildcard

我正在尝试将表中的模式与用户话语进行匹配。

string userUtterance = "I want identification number for number of customers";

string pattern1 = "identification number";

string pattern2 = "tom";

string pattern3 = "id"; 
Run Code Online (Sandbox Code Playgroud)

预期结果:

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
Run Code Online (Sandbox Code Playgroud)

我想就如何使用匹配该语法的正则表达式来限制不明确的匹配和精确匹配字面词提供一些建议。

谢谢

pid*_*pid 6

您可以将 \b用于单词边界:

"\bonly these words\b"
Run Code Online (Sandbox Code Playgroud)

这将匹配这些句子中的这些词:

这里只有这些词宝贝。

这里有“只有这些话”的宝贝。

这里只有这些话,宝贝。

这里只有这些话。

我说:'只有这些话'。