需要 C# 字边界正则表达式而不是 .Contains()

发布于 2024-09-28 04:57:59 字数 453 浏览 3 评论 0原文

我有一个列表:

var myList = new List<string> { "red", "blue", "green" };

我有一个字符串:

var myString = "Alfred has a red and blue tie";

我正在尝试获取 myStringmyList 中单词匹配的计数。目前,我正在使用 .Contains(),它得到的计数为 3,因为它拾取了“Alfred”中的“红色”。我需要能够孤立言语。如何才能实现这一目标?

var count = myList.Where(ml => myString.Contains(ml)); // gets 3, want 2

I have a list:

var myList = new List<string> { "red", "blue", "green" };

I have a string:

var myString = "Alfred has a red and blue tie";

I am trying to get a count of matches of words in myList within myString. Currently, I am using .Contains(), which gets me a count of 3 because it is picking up the "red" in "Alfred". I need to be able to osolate words instead. How can this be achieved?

var count = myList.Where(ml => myString.Contains(ml)); // gets 3, want 2

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

心安伴我暖 2024-10-05 04:57:59
        var myList = new List<string> { "red", "blue", "green" };
        Regex r = new Regex("\\b(" + string.Join("|", myList.ToArray()) + ")\\b");
        MatchCollection m = r.Matches("Alfred has a red and blue tie");

m.Count 将为您提供发现红色、蓝色或绿色的次数。 \b 指定字边界。

m 的每个元素都是匹配类型,您可以查看每个索引以获取更多信息(即 m[0].Value 为您提供匹配的字符串(红色),m[0].Index 为您提供原始字符串中的位置)字符串 (13))。

        var myList = new List<string> { "red", "blue", "green" };
        Regex r = new Regex("\\b(" + string.Join("|", myList.ToArray()) + ")\\b");
        MatchCollection m = r.Matches("Alfred has a red and blue tie");

m.Count will give you the number of times red, blue or green are found. \b specifies word boundary.

Each element of m is of Type Match, and you can look at each index to get more info (ie m[0].Value gives you the matched string (red) and m[0].Index gives you the location in the original string (13)).

怕倦 2024-10-05 04:57:59
var count = (from s in myList
            join ms in myString.Split() on s equals ms
            select new { s, ms }).Count();
var count = (from s in myList
            join ms in myString.Split() on s equals ms
            select new { s, ms }).Count();
时光是把杀猪刀 2024-10-05 04:57:59

像这样的东西吗?

var numMatches = myString.Split().Intersect(myList).Count();

请注意,这不考虑重复出现的情况。

如果您确实想考虑重复,请使用@Justin Niessner 的技术。
这是一个带有中间查找的替代方案:

var words = myString.Split().ToLookup(word => word);
var numMatches = myList.Sum(interestingWord => words[interestingWord].Count());

Something like this?

var numMatches = myString.Split().Intersect(myList).Count();

Note that this doesn't consider duplicate occurrences.

If you do want to consider duplicates, go with @Justin Niessner's technique.
Here's an alternative, with an intermediary lookup:

var words = myString.Split().ToLookup(word => word);
var numMatches = myList.Sum(interestingWord => words[interestingWord].Count());
微凉徒眸意 2024-10-05 04:57:59

这有效
\bred\b|\bblue\b|\bgreen\b
我不确定它是最优化的

this works
\bred\b|\bblue\b|\bgreen\b
I am not sure it is most optimized

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文