Regex - Validation of numeric with up to 4 decimal

2020-06-25 05:10发布

I am having a bit of difficulty with the following:

I need to allow any positive numeric value up to four decimal places. Here are some examples.

Allowed:

123
12345.4
1212.56
8778787.567
123.5678

Not allowed:

-1
12.12345
-12.1234

I have tried the following:

^[0-9]{0,2}(\.[0-9]{1,4})?$|^(100)(\.[0]{1,4})?$

However this doesn't seem to work, e.g. 1000 is not allowed when it should be.

Any ideas would be greatly appreciated.

Thanks

标签: regex
5条回答
家丑人穷心不美
2楼-- · 2020-06-25 05:32

This regex would do the trick:

^\d+(?:\.\d{1,4})?$

From the beginning of the string search for one or more digits. If there's a . it must be followed with atleast one digit but a maximum of 4.

查看更多
聊天终结者
3楼-- · 2020-06-25 05:33

To explain why your attempt is not working for a value of 1000, I'll break down the expression a little:

^[0-9]{0,2}             # Match 0, 1, or 2 digits (can start with a zero)...
(\.[0-9]{1,4})?$        # ... optionally followed by (a decimal, then 1-4 digits)
|                       # -OR-
^(100)                  # Capture 100...
(\.[0]{1,4})?$          # ... optionally followed by (a decimal, then 1-4 ZEROS)

There is no room for 4 digits of any sort, much less 1000 (theres only room for a 0-2 digit number or the number 100)

^\d*                    # Match any number of digits (can start with a zero)
(\.\d{1,4})?$           # ...optionally followed by (a decimal and 1-4 digits)

This expression will pass any of the allowed examples and reject all of the Not Allowed examples as well, because you (and I) use the beginning-of-string assertion ^.

It will also pass these numbers:

.2378
1234567890
12374610237856987612364017826350947816290385
000000000000000000000.0
0

... as well as a completely blank line - which might or might not be desired

to make it reject something that starts with a zero, use this:

^(?!0\d)\d*             # Match any number of digits (cannot "START" with a zero)
(\.\d{1,4})?$           # ...optionally followed by (a decimal and 1-4 digits)

This expression (which uses a negative lookahead) has these evaluations:

REJECTED                Allowed
---------               -------
0000.1234               0.1234
0000                    0
010                     0.0  

You could also test for a completely blank line in other ways, but if you wanted to reject it with the regex, use this:

^(?!0\d|$)\d*(\.\d{1,4})?$
查看更多
一纸荒年 Trace。
4楼-- · 2020-06-25 05:34
^(?<!-)\+?\d+(\.?\d{0,4})?$

The will match something with doesn't start with -, maybe has a + followed by an integer part with at least one number and an optional floating part of maximum 4 numbers.

Note: Regex does not support scientific notation. If you want that too let me know in a comment.

查看更多
forever°为你锁心
5楼-- · 2020-06-25 05:44

If you have a double value but it goes to more decimal format and you want to shorter it to 4 then !

   double value = 12.3457652133
  value =Double.parseDouble(new DecimalFormat("##.####").format(value));
查看更多
啃猪蹄的小仙女
6楼-- · 2020-06-25 05:49

Try this:

^[0-9]*(?:\.[0-9]{0,4})?$

Explanation: match only if starting with a digit (excluding negative numbers), optionally followed by (non-capturing group) a dot and 0-4 digits.

Edit: With this pattern .2134 would also be matched. To only allow 0 < x < 1 of format 0.2134, replace the first * with a + above.

查看更多
登录 后发表回答