正则表达式学习笔记

Regex rx = new Regex(@"1\d*2");
            string s = "123123123";
            mcoll = rx.Matches(s);
            Debug.WriteLine(mcoll.Count);
            string mxs;
            int j = 0;
            foreach (Match mx in mcoll)
            {
                if ((mxs = mx.ToString().Trim()) != "")
                    Debug.WriteLine(string.Format("{0}->{1},{2}", j++, mxs,mx.Index));
            }

 

1\d*2  是贪婪匹配的,只能得到一个结果

0->12312312,0

如果加个?改成懒惰(非贪婪)

1\d*?2 ,可以得到3个结果:

0->12,0
1->12,3
2->12,6

注意两个都不能匹配从开头的12312。

posted @ 2011-05-13 10:28  白金龙  阅读(71)  评论(0编辑  收藏  举报