C# 用于短语搜索的正则表达式
我必须在大字符串中搜索短语,长度可能为500或600或更大,现在我必须检查短语是否存在C# 用于短语搜索的正则表达式,c#,regex,linq,pattern-matching,phrase,C#,Regex,Linq,Pattern Matching,Phrase,我必须在大字符串中搜索短语,长度可能为500或600或更大,现在我必须检查短语是否存在 phrase = "Lucky Draw" big string1 = "I'm looking for Lucky Draw a way to loop through the sentences and check" big string1 = "I'm looking for specialLucky Draw a way to loop through the sentences and
phrase = "Lucky Draw"
big string1 = "I'm looking for Lucky Draw a way to loop through the sentences and check"
big string1 = "I'm looking for specialLucky Draw a way to loop through the sentences and check"
big string3 = "I'm looking for Lucky Draws a way to loop through the sentences and check"
bool success = Regex.Match(message, @"\bLucky Draw\b").Success;
我正在做上述解决方法,但它不能满足所有情况
当我有多个短语时,我要做的是在这种情况下使用linq
,如:
bool success = Regex.Match(message, **arrayofstrings**).Success;
您可以使用循环从短语数组中构建一个大型正则表达式
\b(短语一|短语二|短语三|等)
,然后使用该正则表达式与字符串匹配。哪些情况不匹配?是否要匹配第二个示例中的特别幸运抽奖
如果是这样,很明显你不能使用边界……是的,你是对的,我已经观察到了这一点,现在我只想使用linq**Regex.Match(message,arrayofstring)。Success**Try(?您能否回答第二部分?您可以使用LINQ进行多个测试-如果您只是在搜索匹配的子字符串,为什么要使用正则表达式?请使用注释的正确答案更新您的问题。arrayOfStrings.Any(s=>message.Contains))
。