正则匹配句型

钻井平台小前辈 2012-12-09 08:57:32
正则匹配句型
怎么才能学好
你要怎么才能给我钱
你要怎么做阿佐才能给我钱

请问如何匹配出来呢,只匹配红色汉字部分,并且匹配后要把 "怎么"出现的index和 才能 出现的Index列出来
...全文
131 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
你的选择B 2012-12-09
  • 打赏
  • 举报
回复
引用 3 楼 zhuzhaodan 的回复:
你好,是获得 “怎么”,“才能”在句子中出现的位置,例如 怎么学才能学好 怎么 0 才能 3 另外这里不太明白C# code?1(?=(怎么)) 谢谢了

            string str = @"怎么学才能学好
你要怎么做才能给我钱
你要怎么做阿佐才能给我钱";
            Regex regex = new Regex(@"(?=(怎么))[\u4e00-\u9fa5]*(?=(才能))[\u4e00-\u9fa5]*");
            MatchCollection mc = regex.Matches(str);
            foreach (Match item in mc)
            {
                Console.WriteLine("index:" + item.Groups[1].Index + " " + item.Groups[1].Value + "  index:" + item.Groups[2].Index + " " + item.Groups[2].Value);
            }
  • 打赏
  • 举报
回复
你好,是获得 “怎么”,“才能”在句子中出现的位置,例如 怎么才能学好 怎么 0 才能 3 另外这里不太明白
(?=(怎么))
谢谢了
你的选择B 2012-12-09
  • 打赏
  • 举报
回复
效果如下图:
你的选择B 2012-12-09
  • 打赏
  • 举报
回复

            string str = @"怎么学才能学好
                           你要怎么做才能给我钱
                           你要怎么做阿佐才能给我钱";
            Regex regex = new Regex(@"(?=(怎么))[\u4e00-\u9fa5]*(?=(才能))[\u4e00-\u9fa5]*");
            MatchCollection mc = regex.Matches(str);
            int index = 0;
            foreach (Match item in mc)
            {
                Console.WriteLine("index:"+index+" "+item.Groups[1].Value + "  index:"+ index +" " + item.Groups[2].Value);
                index++;
            }
  • 打赏
  • 举报
回复
好的我看看谢谢
  • 打赏
  • 举报
回复
你好 (?=(怎么)) 这个市怎么理解的不太明白,就是这个分组
你的选择B 2012-12-09
  • 打赏
  • 举报
回复
效果如下图所示(考虑到了回车换行):

(?=(怎么)):正则表达式中的顺序肯定环视,匹配一个位置,这个位置的右侧为"怎么"
建议参考博客:http://blog.csdn.net/lxcnn/article/details/4268033

110,566

社区成员

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

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

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