Regex r = new Regex(@"^\+?0*(?:100(\.0*)?|(\d{0,2}(?=\.\d)|\d{1,2}(?=($|\.$)))(\.\d*)?)$");
涵蓋規則:
00、00.、00.00、001.100 合法1.07f) 未處理測試代碼:
string x = "";
while (true) {
x = Console.ReadLine();
if (x != "exit") {
Console.WriteLine(x + (r.IsMatch(x) ? " succeed!" : " failed!"));
} else break;
}
日期、URL、Email 等精確匹配極難寫出完美正則,建議: