Check decimal and total number length

Using regex , I can check if it is decimal or not

^\d*\.?\d*$ 

But what I want to control is the total length of this figure.

 (^\d*\.?\d*$){1,10} 

But I still can't control him.

After 2 days, my final decision

 (?=^\d*\.?\d*$)^.{1,10}$ 

Regular expression visualization

Demo version of Debuggex

+6
source share
2 answers

Here's the solution:

 ^(?!.{10})\d*\.?\d*$ 

This will verify that the integer is no more than 10 characters.

If you want to check that an integer is at most 10 digits (not counting the period), you can use

 ^((?!.{11})\d*\.\d*|(?!.{10})\d*)$ 
+5
source
 (^\d*\.?\d*$){1,10} โ†‘ โ†‘ 

The value * means zero or more. Therefore, your regular expression means "zero or more times, 1 to 10 times," so {1,10} is redundant in this case. You need to remove * or apply {1,10} only to parts that do not have it.

For example, you can change your regular expression to:

 ^\d*(\.?\d{0,10})$ 

This corresponds to numbers that have up to 10 digits after the period. If you want to combine a floating point number having only 10 digits, you can use a negative lookahead (like @ DenysSรฉguret already mentioned in his +1 answer):

 (?!.{10})\d*\.?\d* 

It matches \d*\.?\d*$ only if its length is <10 (including the point).

+2
source

Source: https://habr.com/ru/post/987909/


All Articles