【发布时间】:2019-09-29 00:16:21
【问题描述】:
C#
string myString = @"'Key:Apple
'Key:Apple123";
string pattern = "('Key:Apple)|('Key: Apple)";
int count = Regex.Matches(myString, pattern).Count;
Console.WriteLine(count); //displaying count as 2
Console.ReadLine();
我只搜索 Apple(不是 Apple123),但我得到的计数仍然为 2。如何仅搜索特定单词“Apple”然后将计数显示为 1?
我尝试了以下方法:
string myString = @"'Key:Apple
'Key:Apple123";
string pattern = "(\\b'Key:Apple\\b)|(\\b'Key: Apple\\b)";
int count = Regex.Matches(myString, pattern).Count;
Console.WriteLine(count); //displaying count as 0
Console.ReadLine();
通过使用上述方法,我们将计数设为零。
请指教。谢谢!
【问题讨论】:
-
试试
'Key: ?Apple\b