当前位置:编程学习 > C#/ASP.NET >>

一个正则表达式问题,谁能解决?马上给份

--------------------编程问答-------------------- foreach(Match m in rx.Matches(youstring))
{
     m.Group["yue"].Value   
} --------------------编程问答-------------------- 错了,你这是匹配到的内容

我要的是:(?<yue>\d+) 和:(?<nian>\d+)

--------------------编程问答-------------------- 少个了s

            foreach (Match m in Regex.Matches(str))
            {
                m.Groups["yue"].Value;                
                m.Groups["nian"].Value;

            }
--------------------编程问答--------------------
引用 2 楼 haorenr 的回复:
错了,你这是匹配到的内容

我要的是:(?<yue>\d+) 和:(?<nian>\d+)
不明白什么意思。 --------------------编程问答-------------------- 既然使用了命名捕获组
那就根据捕获组名称去获取值 
rx.Match("").Groups["组名称"].Value --------------------编程问答-------------------- 我要的结果是返回:(?<yue>\d+) 和:(?<nian>\d+)
而不是匹配后的内容,不知道明白没有
--------------------编程问答-------------------- \(\?[^)]+?\) --------------------编程问答--------------------
引用 6 楼 haorenr 的回复:
我要的结果是返回:(?<yue>\d+) 和:(?<nian>\d+)
而不是匹配后的内容,不知道明白没有


我明白,不过这有什么意义? --------------------编程问答-------------------- 就是动态获这个Group的正则式,而不是匹配到的内容 --------------------编程问答-------------------- 喔 是要得到那两个字符串,这样?

            string str = @"/news/(?<yue>\d+)/(?<nian>\d+)/show-100.aspx";
            Regex reg = new Regex(@"(?i)\(\?[^\)]+\)");
            foreach (Match m in reg.Matches(str))
                Console.WriteLine(m.Value);
/*
(?<yue>\d+)
(?<nian>\d+)
*/
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,