How to use .NET Regex to express positive numbers with decimal [closed]
How can use regular express to express the positive amount with most 20 digits in 开发者_如何学运维integral and 6 decimals, such as 0.25, 1234566789.123456?
Thank you.
var regexStr = @"^\d{1,20}(\.\d{1,6})?$";
var r = Regex.Match("15", regexStr); // match 15
r = Regex.Match("15.158", regexStr); // match 15.158
r = Regex.Match("-22.9", regexStr); // fail, negative
r = Regex.Match("123456789012345678901.1234567", regexStr); // fail, too long
r = Regex.Match("-123456789012345601.123456", regexStr); // fail, negative
r = Regex.Match("123456789012345601.123456", regexStr); // match 123456789012345601.123456
try this: ^\d{1,20}(.\d{1,6})?$
Try this:
(?<![-\d\.]) \d{1,20} (\.\d{1,6})? \b
Test cases: 0.25, 1234566789.123456, 5.66, -12345678901234567890.1, 12345678901234567890.1, 5
There you go : @"^[0-9]{1,20}(\.[0-9]{1,6})?$"
精彩评论