正则表达式的问题

小西是二愣子 2013-02-04 02:40:16
实现函数 int GetStrCount(string str1,string str2);
例如;str1:"abc" str2:"ab" 得到1
str1:"aaa" str2:"aa" 得到2,说白了就是str2在str1中出现的次数。

我的代码:
Regex RegexWords = new Regex(str2);
int WordCount = RegexWords.Matches(str1).Count;但是在 str1:"aaa" str2:"aa"下还是得到1,好久没写正则了,忘了好多
...全文
356 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
引用 9 楼 gxingmin 的回复:
不用正则,用linq也可以 C# code?1234 string s1 = "aaaa"; string s2 = "aa"; var count = s1.Select((x, i) => new { x, i }).Where(t => t.i <= s1.Length - b.Length &a……
======================================================= .NET快速开发整合框架(RDIFramework.NET),基于.NET的快速开发、整合框架,给用户和开发者最佳的.Net框架部署方案。 平台博客:[CNBLOGS]http://www.cnblogs.com/huyong [CSDN]http://blog.csdn.net/chinahuyong 交流QQ:406590790 (请注明:CSDN) 平台微博:http://t.qq.com/yonghu86 邮件交流:406590790@qq.com
小西是二愣子 2013-02-04
  • 打赏
  • 举报
回复
非正则表达式,解法: private int GetStrCount(string str1, string str2) { int len = str2.Length; int count = 0; for (int i = 0; i+len <= str1.Length; i++) { if (str1.Substring(i, len) == str2) { count++; } } return count; }
小西是二愣子 2013-02-04
  • 打赏
  • 举报
回复
已经结贴 谢谢 hjywyj
gxingmin 2013-02-04
  • 打赏
  • 举报
回复
不用正则,用linq也可以


            string s1 = "aaaa";
            string s2 = "aa";

            var count = s1.Select((x, i) => new { x, i }).Where(t => t.i <= s1.Length - b.Length && s1.Substring(t.i, b.Length) == s2).Count();

小西是二愣子 2013-02-04
  • 打赏
  • 举报
回复
引用 6 楼 hjywyj 的回复:
引用 4 楼 huda3016 的回复:引用 1 楼 hjywyj 的回复:string str1 = "aaaa"; string str2 = "aa"; string pattern = @"(?is)(?=.*?" + str2 + ")."; int n = Regex.Matches……
哥们 不错啊,OK了 能讲下原理吗?
小西是二愣子 2013-02-04
  • 打赏
  • 举报
回复
引用 5 楼 Chinajiyong 的回复:
引用 4 楼 huda3016 的回复:引用 1 楼 hjywyj 的回复:string str1 = "aaaa"; string str2 = "aa"; string pattern = @"(?is)(?=.*?" + str2 + ")."; int n = Regex.Matches……
代码逻辑肯定好解决,我就是想用正则表达式
  • 打赏
  • 举报
回复
引用 4 楼 huda3016 的回复:
引用 1 楼 hjywyj 的回复:string str1 = "aaaa"; string str2 = "aa"; string pattern = @"(?is)(?=.*?" + str2 + ")."; int n = Regex.Matches(str1, pattern).Coun……
string str1 = "aaaaaa"; string str2 = "aa"; string pattern = @"(?is)(?!"+str2+")."; int n =str1.Length- Regex.Matches(str1, pattern).Count;
EnForGrass 2013-02-04
  • 打赏
  • 举报
回复
引用 4 楼 huda3016 的回复:
引用 1 楼 hjywyj 的回复:string str1 = "aaaa"; string str2 = "aa"; string pattern = @"(?is)(?=.*?" + str2 + ")."; int n = Regex.Matches(str1, pattern).Coun……
这个根本不适合用正则处理,自己用代码逻辑处理
小西是二愣子 2013-02-04
  • 打赏
  • 举报
回复
引用 1 楼 hjywyj 的回复:
string str1 = "aaaa"; string str2 = "aa"; string pattern = @"(?is)(?=.*?" + str2 + ")."; int n = Regex.Matches(str1, pattern).Count;
--------------------------------------------------------------- str1 = "aa1aa", str2 = "aa";情况下你的代码得到的是4 ,正确应该是2
wangchengh 2013-02-04
  • 打赏
  • 举报
回复
首先你需要aaa得到2用正则是搞不定的。 因为正则表达式为了效率是顺序检测的,不会回溯,只有aaaa才可能用正则得到2.
  • 打赏
  • 举报
回复
如果str2有\\,\n,\"之类的转义字符,是需要先处理一下的
  • 打赏
  • 举报
回复
string str1 = "aaaa"; string str2 = "aa"; string pattern = @"(?is)(?=.*?" + str2 + ")."; int n = Regex.Matches(str1, pattern).Count;

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧