如何使用.net正则表达式用十进制表示正数

本文关键字:表示 十进制 何使用 net 正则表达式 | 更新日期: 2023-09-27 18:09:26

如何用正则表达式表示整数中最多20位,6位小数的数,如0.25、1234566789.123456?

谢谢。

如何使用.net正则表达式用十进制表示正数

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

试试这个:^ ' d {20} (' d {1,6}) ?$

试试这个:

(?<![-'d'.])  'd{1,20} ('.'d{1,6})? 'b

测试用例:0.25, 1234566789.123456, 5.66, -12345678901234567890.1, 12345678901234567890.1, 5

好了:@ " ^ [0 - 9]{1,20}(' [0 - 9]{1,6})?$ "