我是使用正则表达式和搜索文本的新手。我想在一个文本中同时搜索多个单词,并且我正在考虑执行以下操作
string text = new TextRange(rtfMain.Document.ContentStart, rtfMain.Document.ContentEnd).Text;
string[] searchWords = { "Danny", "Lucy", "Marco" };
Regex rx = new Regex( searchWords,
RegexOptions.Compiled | RegexOptions.IgnoreCase);
MatchCollection matches = rx.Matches(text);
foreach (Match match in matches)
{
ResultList.Add(match.ToString());
}
但是正则表达式似乎不接受变量
string[] searchWords
我如何使它起作用,或者我的方法在实现我所需要的方面是错误的?
将搜索词连接到一个字符串中,每个字符串之间用分隔|
。
string searchWords = "Danny|Lucy|Marco";
Regex rx = new Regex( searchWords,
RegexOptions.Compiled | RegexOptions.IgnoreCase);
如果需要数组,请动态连接字符串。
string[] searchWordsArray = { "Danny", "Lucy", "Marco" };
string searchWords = String.Join("|", searchWordsArray)
Regex rx = new Regex( searchWords,
RegexOptions.Compiled | RegexOptions.IgnoreCase);
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句