正则表达式仅用于验证数字和点 [英] Regex for validating only numbers and dots

查看:79
本文介绍了正则表达式仅用于验证数字和点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个只接受数字和点的java正则表达式。

I want a java regex expression that accepts only the numbers and dots.

例如,

             1.1.1 ----valid
             1.1   ----valid
             1.1.1.1---valid
             1.    ----not valid

点不应位于起始位置或结束位置。

The dots should not be at the starting position or at the ending position.

推荐答案

我想这就是你想要的:

^\d+(\.\d+)*$

说明:它接受以点分隔的数字;它以数字开头和结尾;一个数字可以有多个数字;也可以接受一个没有点的数字。

Explanation: It accepts numbers separated by dots; it starts and ends with number; a number can have multiple digits; one number without dots is also accepted.

没有多位数的变体:

^\d(\.\d)*$

变量至少有一个需要点:

Variants where at least one dot is required:

^\d+(\.\d+)+$
^\d(\.\d)+$

不要忘记在Java中你必须逃脱\符号,所以代码如下所示:

Don't forget that in Java you have to escape the \ symbols, so the code will look like this:

Pattern NUMBERS_WITH_DOTS = Pattern.compile("^\\d+(\\.\\d+)*$");

这篇关于正则表达式仅用于验证数字和点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆