c# - Find equal substring in list of strings
问题描述
I'm trying to figure out, how to find equal sub-string in big list of strings.
This method works fine:
var results = myList.FindAll(delegate (string s) { return s.Contains(myString); });
But it also looks for sub-string with part of word, for example, if I'm looking for "you do" it founds also extra "you dont" because contains "you do.."
In case of string, this method seems gives desired result:
bool b = str.Contains(myString);
if (b)
{
int index = str.IndexOf(myString);
}
How to get same kind of matching with list
解决方案
您可以使用正则表达式返回一组潜在术语的所有匹配项:
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));
}
推荐阅读
- javascript - 如何仅将 DOMParser 结果的特定部分附加到 DocumentFragment 元素
- reactjs - 在 React js 中显示本地图像并将其保存到数据库中
- javascript - 我正在尝试从谷歌电子表格发送一封电子邮件,但是该邮件并未复制超链接
- ios - Ionic 4:侧边菜单(ion-menu)在 IOS 中没有正确关闭
- flutter - 在 Flutter 中使用 Firebase 然后(Instagram)进行正确的登录架构
- python - 如何使用 Python 使用 Facebook Graph API 分享其他人的帖子?
- html - 如何调整导航栏高度并为链接添加悬停颜色?
- ip-address - 使用多个 IP 地址流式传输内容
- c++ - 修改自定义 C++ Bazel 规则中的包含路径
- javascript - avatar cmd, .setImage 头像不显示为 gif