首页 > 解决方案 > 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

标签: c#regexlinqsubstringcontains

解决方案


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

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));
}

在线尝试


推荐阅读