在字符串列表中找到相等的子字符串 [英] Find equal substring in list of strings

查看:139
本文介绍了在字符串列表中找到相等的子字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试找出如何在大字符串列表中找到相等的子字符串.

I'm trying to figure out, how to find equal sub-string in big list of strings.

此方法可以正常工作:

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

如何获得与列表的匹配

推荐答案

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

You could use regular expressions to return all of the matches for a set of potential terms:

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?");

如果只有一个词,则可以将其重写为:

If you only have one term you can rewrite this as:

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](对于match集合中的每个组)进行匹配以获取匹配的值:

And then you can match use match.Groups[0] (for each group in the match collection) to get the matched value:

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

在线试用

这篇关于在字符串列表中找到相等的子字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆