What#39;s the shortest regex that can match non-zero floating point numbers with any number of decimal places?(可以匹配具有任意小数位数的非零浮点数的最短正则表达式是什么?)
问题描述
可以匹配任意小数位数的非零浮点数的最短正则表达式是什么?
What's the shortest regex that can match non-zero floating point numbers with any number of decimal places?
它应该接受类似的数字
-1
-5.9652
-7.00002
-0.8
-0.0500
-0.58000
0.01
0.000005
0.9900
5
7.5
7.005
但拒绝诸如
.
.02
-.
-.996
0
-0
0.
-0.
-0.000
0.00
--
..
+
+0
+1
+.
+1.26
,etc
我不需要支持科学记数法,例如 e 、 E 等.
顺便说一下,我使用的语言是 C#.
I do not need support for the scientific notation, with e , E and such.
The language I'm using is C#, by the way.
推荐答案
^-?(0.d*[1-9]|[1-9]d*(.d+)?)$
<小时>
编辑已更新以反映新要求(最后一位小数可以为零)
EDIT Updated to reflect new requirements (last decimals can be zero)
^-?(0.d*[1-9]d*|[1-9]d*(.d+)?)$
(比使用前瞻更短:^-?(0.(?=[1-9])d*|[1-9]d*(.d+)?)$代码>.)
(Shorter than using lookahead: ^-?(0.(?=[1-9])d*|[1-9]d*(.d+)?)$
.)
EDIT2 如果例如001.000
可以通过
^-?(?=.*[1-9])d+(.d+)?$
这篇关于可以匹配具有任意小数位数的非零浮点数的最短正则表达式是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:可以匹配具有任意小数位数的非零浮点数的最短正则表达式是什么?


基础教程推荐
- 在 VS2010 中的 Post Build 事件中将 bin 文件复制到物 2022-01-01
- 首先创建代码,多对多,关联表中的附加字段 2022-01-01
- 是否可以在 asp classic 和 asp.net 之间共享会话状态 2022-01-01
- 全局 ASAX - 获取服务器名称 2022-01-01
- 从 VS 2017 .NET Core 项目的发布目录中排除文件 2022-01-01
- 错误“此流不支持搜索操作"在 C# 中 2022-01-01
- 如何动态获取文本框中datagridview列的总和 2022-01-01
- 经典 Asp 中的 ResolveUrl/Url.Content 等效项 2022-01-01
- JSON.NET 中基于属性的类型解析 2022-01-01
- 将事件 TextChanged 分配给表单中的所有文本框 2022-01-01