开发者

How to use .NET Regex to express positive numbers with decimal [closed]

开发者 https://www.devze.com 2023-04-06 10:23 出处:网络
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical andcannot be reasonably answered in its current form. For help clari
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.

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})?$"

0

精彩评论

暂无评论...
验证码 换一张
取 消