我需要在.net中使用正则表达式
示例:
无效的数字:
答案 0 :(得分:2)
^100\.00|\d{1,2}\.(?:11|10|0\d)$
好的,比我想象的要简单。
^100(?:\.00?)?$|^(?:\d{1,2})?(?:\.(?:1|11|10|0\d?))?$
这匹配新的数据集。它目前也匹配.0。
答案 1 :(得分:1)
这个正则表达式是基于Xetius的那个:
^(100(?:\.00?)?|\d{1,2}(?:\.(?:11|10|0\d|0|1))?)$
下面是我测试它的代码:
class Program
{
const string Pattern = @"^(100(?:\.00?)?|\d{1,2}(?:\.(?:11|10|0\d|0|1))?)$";
static void Main(string[] args)
{
for (int before = 0; before < 100; before++)
{
Test(before.ToString());
for (int after = 0; after < 12; after++)
{
Test(string.Format("{0}.{1:d2}", before, after));
Test(string.Format("{0:d2}.{1:d2}", before, after));
Test(string.Format(".{0:d2}", after));
}
Test(string.Format("{0}.{1:d}", before, 0));
Test(string.Format("{0:d2}.{1:d}", before, 1));
}
// Special cases:
Test("100");
Test("100.0");
Test("100.00");
// intended to fail
Test("00.20");
Test("00.12");
Test("00.00x");
Test("000.00");
Console.WriteLine("done.");
Console.ReadLine();
}
private static void Test(string input)
{
Regex r = new Regex(Pattern);
if (!r.IsMatch(input))
{
Console.WriteLine("no match: " + input);
}
}
}
答案 2 :(得分:0)
^(100(\.00?)?|\d\d?(\.(0\d|1[01]?))?)$
答案 3 :(得分:0)
^(?:\d{1,2}(?:\.(?:0?\d|1[01]))?|100)$
答案 4 :(得分:0)
"^100(\.00?)?|\d\d?(\.(1[01]?|0\d?))?$"