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());//匹配的數據輸出。
}