C# 在字符串列表中查找相等的子字符串

C# 在字符串列表中查找相等的子字符串,c#,regex,linq,substring,contains,C#,Regex,Linq,Substring,Contains,我想弄清楚,如何在大字符串列表中找到相等的子字符串 这种方法效果很好: var results = myList.FindAll(delegate (string s) { return s.Contains(myString); }); 但它也会查找包含单词一部分的子字符串,例如,如果我查找“youdo”,它会发现额外的“youdont”,因为其中包含“youdo…” 对于字符串,此方法似乎给出了所需的结果: bool b = str.Contains(myString); if (b)

我想弄清楚,如何在大字符串列表中找到相等的子字符串

这种方法效果很好:

var results = myList.FindAll(delegate (string s) { return s.Contains(myString); });
但它也会查找包含单词一部分的子字符串,例如,如果我查找“youdo”,它会发现额外的“youdont”,因为其中包含“youdo…”

对于字符串,此方法似乎给出了所需的结果:

 bool b = str.Contains(myString);
 if (b)
 {
     int index = str.IndexOf(myString);    
 }

如何获得与列表相同类型的匹配

您可以使用正则表达式返回一组潜在术语的所有匹配项:

string[] stringsToTest = new [] { "you do", "what" };
var escapedStrings = stringsToTest.Select(s => Regex.Escape(s)); // escape the test strings so that we can safely build them into the expression
var regex = new Regex("\\b(" + string.Join("|", escapedStrings) + ")\\b");
var matches = regex.Matches("How you do? How you don't? What you do? How you do what you do?");
如果您只有一个术语,您可以将其改写为:

var regex = new Regex(string.Format("\\b({0})\\b", Regex.Escape("you do")));
var matches = regex.Matches("How you do? How you don't? What you do? How you do what you do?");
然后您可以使用
match.Groups[0]
(对于匹配集合中的每个组)进行匹配,以获得匹配的值:

foreach (Match m in matches)
{
    Console.WriteLine(string.Format("Matched {0} at {1}", m.Groups[0].Value, m.Groups[0].Index));
}

您可以使用正则表达式返回一组潜在术语的所有匹配项:

string[] stringsToTest = new [] { "you do", "what" };
var escapedStrings = stringsToTest.Select(s => Regex.Escape(s)); // escape the test strings so that we can safely build them into the expression
var regex = new Regex("\\b(" + string.Join("|", escapedStrings) + ")\\b");
var matches = regex.Matches("How you do? How you don't? What you do? How you do what you do?");
如果您只有一个术语,您可以将其改写为:

var regex = new Regex(string.Format("\\b({0})\\b", Regex.Escape("you do")));
var matches = regex.Matches("How you do? How you don't? What you do? How you do what you do?");
然后您可以使用
match.Groups[0]
(对于匹配集合中的每个组)进行匹配,以获得匹配的值:

foreach (Match m in matches)
{
    Console.WriteLine(string.Format("Matched {0} at {1}", m.Groups[0].Value, m.Groups[0].Index));
}

最简单的方法可能是使用正则表达式(例如,
\byou do\b
)@John您好,我不确定使用正则表达式在大字符串列表中查找子字符串,必须是每个字符串,我猜列表有多大?@John大约50000个字符串最简单的方法可能是使用正则表达式(例如,
\byou do\b
)@John您好,我不确定是否使用正则表达式在大字符串列表中查找子字符串,我猜每个字符串的列表有多大?@John about 50000 stringsSo,如果我的列表
list myList=new list();
包含大约50000个字符串,如“你怎么做?你怎么不做?你怎么做?你怎么做?你怎么做?”,那么在这种情况下,每个字符串都必须在循环中使用
var matches=regex.matches(myString)
进行处理,对吗?你确定,对于这种情况,这不是一个困难的处理吗?使用
IndexOf
(不是您当前使用的
包含
),然后检查匹配字符串后的下一个字符。这取决于您真正需要的效率。测试它。如果它太慢,请进行优化。您的意思是
使用
委派
进行索引?我是指您在下面提供的代码“对于字符串,这个方法似乎给出了期望的结果:“因此,如果我的列表
list myList=new list();
包含大约50000个字符串,比如“你怎么做?你怎么做?你怎么做?你怎么做?”,那么在这种情况下,每个字符串都必须使用
var matches=regex.matches(myString)进行处理
在循环中,对吗?您确定,对于这种情况,这不是一个困难的处理过程吗?使用
IndexOf
(不
包含当前使用的
),然后检查匹配字符串后的下一个字符。这取决于您真正需要的效率。测试它。如果速度太慢,请进行优化。您是说
IndexOf
with
delegate
?我的意思是“如果是字符串,此方法似乎会给出所需的结果:”