正则表达式匹配浮点数

时间:2021-04-24 11:34:53

I have this pattern:

我有这种模式:

[0-9]*\.?[0-9]

It matches numbers but it also matches 3.5.4 as:

它匹配数字但它也匹配3.5.4:

  1. 3.5
  2. 3.5
  3. .4
  4. 0.4

How to fix that(this input shouldn't be matched)?
UPDATE:
this also should work on input: 1 3.5.4 10

如何解决(这个输入不应该匹配)?更新:这也应该适用于输入:1 3.5.4 10

1.2. is not allowed
.3 is allowed

1.2。是不允许的.3是允许的

any char that is not poart of numer is not allowed, like: a1 2.4f 3. 45, 67!

任何不是numer的字符都是不允许的,例如:a1 2.4f 3. 45,67!

5 个解决方案

#1


10  

Updated answer after comment from poster:

海报评论后更新的答案:

Use lookahead / lookbehind to make sure that the characters before and after are spaces:

使用lookahead / lookbehind确保前后的字符是空格:

Here's a version that closely matches yours, but that won't make partial matches:

这是一个与您的版本非常匹配的版本,但不会进行部分匹配:

(?:^|(?<=\s))[0-9]*\.?[0-9](?=\s|$)

For both these examples, when run on the string 1 2.3.4 5.6 it matches only 1 and 5.6.

对于这两个示例,当在字符串1 2.3.4 5.6上运行时,它仅匹配1和5.6。

#2


12  

To match a json number:

要匹配json数字:

^[-]?(0|[1-9][0-9]*)(\.[0-9]+)?([eE][+-]?[0-9]+)?$

正则表达式匹配浮点数

Use this regex to match .123:

使用此正则表达式匹配.123:

^[-]?((0|[1-9][0-9]*)(\.[0-9]+)?|\.[0-9]+)([eE][+-]?[0-9]+)?$

#3


7  

You have to decide if you want to accept numbers without leading zeros (eg .123). If you don't then the regex is easy:

您必须决定是否要接受不带前导零的数字(例如.123)。如果你不这样,那么正则表达式很简单:

^-?[0-9]+(\.[0-9]+)?$

If you do then it's a little more complex:

如果你这样做,它会更复杂一些:

^-?(?:[0-9]+|[0-9]*\.[0-9]+)$

Both presume that a decimal point must be followed by at least one digit. If you don't accept negative numbers then the leading -? is unnecessary.

两者都假设小数点后面必须至少跟一个数字。如果你不接受负数那么领先 - ?是不必要的。

#4


2  

Your regex is not anchored. If you want to match lines that contain only numbers and nothing else use:

你的正则表达式没有锚定。如果要匹配仅包含数字的行,则不使用其他内容:

^[0-9]*\.?[0-9]$

#5


1  

You should not use a regex for this unless you really need to.

除非你真的需要,否则你不应该使用正则表达式。

Instead, you should use your language's built-in number parsing methods.

相反,您应该使用您的语言的内置数字解析方法。

I assume from your other questions that you're using Javascript; if so, you should call parseFloat.

我从你的其他问题中假设你正在使用Javascript;如果是这样,你应该调用parseFloat。

#1


10  

Updated answer after comment from poster:

海报评论后更新的答案:

Use lookahead / lookbehind to make sure that the characters before and after are spaces:

使用lookahead / lookbehind确保前后的字符是空格:

Here's a version that closely matches yours, but that won't make partial matches:

这是一个与您的版本非常匹配的版本,但不会进行部分匹配:

(?:^|(?<=\s))[0-9]*\.?[0-9](?=\s|$)

For both these examples, when run on the string 1 2.3.4 5.6 it matches only 1 and 5.6.

对于这两个示例,当在字符串1 2.3.4 5.6上运行时,它仅匹配1和5.6。

#2


12  

To match a json number:

要匹配json数字:

^[-]?(0|[1-9][0-9]*)(\.[0-9]+)?([eE][+-]?[0-9]+)?$

正则表达式匹配浮点数

Use this regex to match .123:

使用此正则表达式匹配.123:

^[-]?((0|[1-9][0-9]*)(\.[0-9]+)?|\.[0-9]+)([eE][+-]?[0-9]+)?$

#3


7  

You have to decide if you want to accept numbers without leading zeros (eg .123). If you don't then the regex is easy:

您必须决定是否要接受不带前导零的数字(例如.123)。如果你不这样,那么正则表达式很简单:

^-?[0-9]+(\.[0-9]+)?$

If you do then it's a little more complex:

如果你这样做,它会更复杂一些:

^-?(?:[0-9]+|[0-9]*\.[0-9]+)$

Both presume that a decimal point must be followed by at least one digit. If you don't accept negative numbers then the leading -? is unnecessary.

两者都假设小数点后面必须至少跟一个数字。如果你不接受负数那么领先 - ?是不必要的。

#4


2  

Your regex is not anchored. If you want to match lines that contain only numbers and nothing else use:

你的正则表达式没有锚定。如果要匹配仅包含数字的行,则不使用其他内容:

^[0-9]*\.?[0-9]$

#5


1  

You should not use a regex for this unless you really need to.

除非你真的需要,否则你不应该使用正则表达式。

Instead, you should use your language's built-in number parsing methods.

相反,您应该使用您的语言的内置数字解析方法。

I assume from your other questions that you're using Javascript; if so, you should call parseFloat.

我从你的其他问题中假设你正在使用Javascript;如果是这样,你应该调用parseFloat。