在字符串列表中查找相等的子字符串
Find equal substring in list of strings
我想知道如何在大字符串列表中找到相等的子字符串。
这个方法很好用:
var results = myList.FindAll(delegate (string s) { return s.Contains(myString); });
但它也会查找带有部分单词的子字符串,例如,如果我正在查找 "you do",它还会找到额外的 "you dont",因为包含 "you do.."
对于字符串,此方法似乎给出了预期的结果:
bool b = str.Contains(myString);
if (b)
{
int index = str.IndexOf(myString);
}
如何获得与列表相同类型的匹配
您可以使用正则表达式 return 一组潜在术语的所有匹配项:
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));
}
我想知道如何在大字符串列表中找到相等的子字符串。
这个方法很好用:
var results = myList.FindAll(delegate (string s) { return s.Contains(myString); });
但它也会查找带有部分单词的子字符串,例如,如果我正在查找 "you do",它还会找到额外的 "you dont",因为包含 "you do.."
对于字符串,此方法似乎给出了预期的结果:
bool b = str.Contains(myString);
if (b)
{
int index = str.IndexOf(myString);
}
如何获得与列表相同类型的匹配
您可以使用正则表达式 return 一组潜在术语的所有匹配项:
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));
}