C#使用正则表达式检查字符串中重复出现的词

复制代码
 private void button1_Click(object sender, EventArgs e)
        {
            MatchCollection matches =//使用正则表达式查找重复出现单词的集合
                Regex.Matches(label1.Text,
                @"\b(?<word>\w+)\s+(\k<word>)\b", RegexOptions.Compiled | RegexOptions.IgnoreCase);
            if (matches.Count != 0)//如果集合中有内容
            {
                foreach (Match//遍历集合
                    match in matches)
                {
                    string word = match.Groups["word"].Value;//获取重复出现的单词
                    MessageBox.Show(word.ToString(), "英文单词");//弹出消息对话框
                }
            }
            else { MessageBox.Show("没有重复的单词"); }//弹出消息对话框
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            label1.Text =//创建字符串对象
                "The the quick brown fox  fox jumped over the lazy dog dog.";
        }
复制代码

 

posted @   明志德道  阅读(433)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 在鹅厂做java开发是什么体验
· 百万级群聊的设计实践
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
· 永远不要相信用户的输入:从 SQL 注入攻防看输入验证的重要性
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
点击右上角即可分享
微信分享提示