Regular expression for decimal places with maximum length

I'm not sure if this is possible with a regex. I will try to use the regex, but if this is not possible, I will switch to double check.

My database (postgresql) accepts decimal as 15.6 (maximum 15 digits with maximum six decimal places), so if I have 10 integer digits, I can have 5 decimal digits. The decimal separator is ignored.

I am currently using this regex (Comma is the decimal separator):

^\d{1,15}(\,\d{1,6})?$

It does not check the total length, but only the numbers on the left. But since the user can also enter dots (thousands separators), I have this monster:

^((\d+)|(\d{1,3}(\.\d{3})+)|(\d{1,3}(\.\d{3})(\,\d{3})+))((\,\d{4})|(\,\d{3})|(\,\d{2})|(\,\d{1})|(\,))?$

It does not accept more than three decimal places. I can edit this to accept 6 decim, but I still can’t check the total length.

Can I check the total number length? Ignoring dots and semicolons.

The regular expression should take:

1234567890,123456

1.234.567.890,123456

And of course, any other averages:

1.234,12 , 1,0 , 1 ...

+5
source share
3 answers

I think you need

 ^(?:\d{1,3}(?:\.\d{3}){0,4}|\d{1,15})(?:,\d{1,6})?$ 

Watch the regex demo

The integer part allows only 1 to 15 digits (or 5 groups of 3-digit fragments, separated by a digit grouping symbol . ) And optionally with a comma and from 1 to 6 decimal digits.

  • ^ - start of line
  • (?:\d{1,3}(?:\.\d{3}){0,4}|\d{1,15}) - 2 alternatives:
    • \d{1,3}(?:\.\d{3}){0,4} - from 1 to 3 digits, and then from 0 to 4 sequences of dots ( \. ) and 3 digits ( \d{3} )
    • | - or
    • \d{1,15} - from 1 to 15 digits
  • (?:,\d{1,6})? - an optional sequence (1 or 0 occurrences) of a comma followed by 1 to 6 digits
  • $ - end of line
+2
source

Just throw it away as an alternative. In my opinion, you should not use Regex in this case. What for? Because the regex that really matches this question will not be easy to understand.

There are much simpler solutions to the problem. Take for example the following:

 var input = "1.234.567.890,123456"; var input2 = Regex.Replace(input, @"[^\d,]+", ""); var split = input2.Split(','); var totalLength = split[0].Length + split[1].Length; 

Nothing unusual. But it works, unlike other solutions provided so far.

But what about the different culture settings? Different cultures use different thousands of separators. Something like Double.TryParse may appear:

 var input = "1.234.567.890,123456"; var style = NumberStyles.Number; var culture = CultureInfo.CreateSpecificCulture("nl-NL"); double result; if (double.TryParse(input, style, culture, out result)) { Console.WriteLine("It worked!"); } else { Console.WriteLine("Not valid"); } 

The above works because the nl-NL culture uses the format. Install it on en-US and it will not work.

None of them use Regex to complete the task, but they can still give the desired result.

+3
source

Try the following: ^[\d\.\,]{1,15}([\.\,]\d{0,6})?$

0
source

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


All Articles