regex - 正则表达式只允许小数和 0 中的负数
问题描述
需要一个只允许 0 和负数的正则表达式。下面的表达式也允许正数。
^-?[0-9]\d*(\.\d+)?$
解决方案
我会在这里使用一个替代:
^(?:0(?:\.0+)?|-\d+(?:\.\d+)?)$
演示
^(?: from the start of the input
0(?:\.0+)? match 0, or 0.0, 0.00 etc.
| OR
-\d+(?:\.\d+)? match a negative number, with optional decimal component
)$ end of the input
推荐阅读
- zsh - 如何在每个命令后隐藏 zsh 中显示的经过时间
- java - Java从UTF-8中的URL读取XML?
- python-3.x - 如何删除并仅保留某些非字母数字字符?
- regex - 正则表达式如果第一次匹配,则显示第二次匹配的结果
- angular - 输入'MonoTypeOperatorFunction
' 不能分配给 'OperatorFunction 类型的参数 - performance - 使用 -gdwarf-2 编译时程序更快
- bash - 如何使用 Bash 在另一个文本文件的末尾添加一列?
- c++ - 添加了 SDL 库:使用 VS 构建时“GNUC 未定义为预处理器宏”
- apache-kafka - 将消息加入 Kafka Stream 后从流中清除消息
- excel - 找到最后添加的工作表 (vba)?