正则表达式数字和小数点[重复]

时间:2022-11-03 16:58:15

This question already has an answer here:

这个问题在这里已有答案:

Could any of you help me with a regex to allow this:

你们中的任何一个人都可以用正则表达式来帮助我:

123.123123.1323.21.23121,23123123,32123,123412

so to sum up: only one decimal point, it can be the last one.. but most not be the first.

总结一下:只有一个小数点,它可以是最后一个......但大多数不是第一个。

but not this:

但不是这个:

12.323.3231213..12331...3123.12313,123132 

4 个解决方案

#1


This should work

这应该工作

^\d+[\.,]?\d*$

^ start of the string
\d+ 1 or more digits
[\.,]? 0 or 1 decimal or comma
\d* 0 or more digits
$ end of string.

^字符串开头\ d + 1位或更多位数[\。,]? 0或1十进制或逗号\ d * 0或更多数字$ end of string。

#2


\b\d+(\.|,)\d*\b

which means:

a word boundarya digit one or more timesa `.` or a `,`a digit zero or more timesa word boundary

The word boundaries are there to prevent the regex matching from the middle of a number

单词边界用于防止正则数字中间的正则表达式匹配

#3


Try this, it works with all cases that you mention.

试试这个,它适用于你提到的所有情况。

(\d+)(((\.|,)\d+)|,))

#4


My fix for your regex: (\d+)(((.|,)\d+)|,)?Added '?' to end, it will works for zero value

我对你的正则表达式的修复:( \ d +)(((。|,)\ d +)|,)?添加'?'结束,它将适用于零价值

#1


This should work

这应该工作

^\d+[\.,]?\d*$

^ start of the string
\d+ 1 or more digits
[\.,]? 0 or 1 decimal or comma
\d* 0 or more digits
$ end of string.

^字符串开头\ d + 1位或更多位数[\。,]? 0或1十进制或逗号\ d * 0或更多数字$ end of string。

#2


\b\d+(\.|,)\d*\b

which means:

a word boundarya digit one or more timesa `.` or a `,`a digit zero or more timesa word boundary

The word boundaries are there to prevent the regex matching from the middle of a number

单词边界用于防止正则数字中间的正则表达式匹配

#3


Try this, it works with all cases that you mention.

试试这个,它适用于你提到的所有情况。

(\d+)(((\.|,)\d+)|,))

#4


My fix for your regex: (\d+)(((.|,)\d+)|,)?Added '?' to end, it will works for zero value

我对你的正则表达式的修复:( \ d +)(((。|,)\ d +)|,)?添加'?'结束,它将适用于零价值