正则表达式限制输入整数或小数

string pattern = @"^\d{1,7}(?:\.\d{0,2}$|$)"; //这是一个格式匹配字符串 其中的含义可以参考http://hi.baidu.com/%D6%C2%D0%F9%B8%F3/blog/item/9060fe35f84f872370cf6c83.html。如果调用以下代码,可以匹配整数位最多为7位,小数位最多为2位的数值型数据(也就是只能输入数字和小数点)
            string text = "12333.689";
            MatchCollection matches = Regex.Matches(text, pattern, RegexOptions.IgnoreCase);
            Console.WriteLine(matches.Count);//输出几处匹配
            foreach (Match match in matches)
            {
                Console.WriteLine(match.ToString());//匹配的数据输出。
            }

posted @ 2012-07-09 23:37  Dample  阅读(4826)  评论(0编辑  收藏  举报