具有最大长度的小数的正则表达式

时间:2022-12-23 22:18:10

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

我不确定这是否可以使用正则表达式。我将尝试使用正则表达式,但如果不可能,我将切换到双重验证。

My database (postgresql) accepts decimal as 15,6 (Maximum of 15 digits with a maximum of 6 decimals), so if I have 10 integer digits, I can have 5 decimals digits. The decimal separator is ignored.

我的数据库(postgresql)接受十进制为15,6(最多15位,最多6位小数),所以如果我有10个整数位,我可以有5位小数。小数分隔符被忽略。

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

我目前有这个正则表达式(逗号是小数点分隔符):

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

It does not verify the total length, only the numbers to the left. But since the user can also type dots (thousands separator), 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})|(\,))?$

This one does not accept more than 3 decimals. I can edit this one to accept 6 decimais, but I still can't validate the total length.

这个不接受超过3位小数。我可以编辑这个以接受6次decimais,但我仍然无法验证总长度。

Is it possible to verify the total length of the number? Ignoring the dots and comma.

是否可以验证号码的总长度?忽略点和逗号。

The regex should accept:

正则表达式应该接受:

1234567890,123456

1.234.567.890,123456

And of course any other middle values:

当然还有其他任何中间价值观:

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

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

3 个解决方案

#1


2  

I think you need

我想你需要

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

See the regex demo

请参阅正则表达式演示

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

整数部分仅允许1到15位数字(或5组3位数字块,用。digit分组符号分隔),并且可选地后跟逗号和1到6个十进制数字。

  • ^ - start of string
  • ^ - 字符串的开头

  • (?:\d{1,3}(?:\.\d{3}){0,4}|\d{1,15}) - 2 alternatives:
    • \d{1,3}(?:\.\d{3}){0,4} - 1 to 3 digits, followed with 0 to 4 sequences of a dot (\.) and 3 digits (\d{3})
    • \ d {1,3}(?:\。\ d {3}){0,4} - 1到3位数字,后跟0到4个点(\。)和3位数字(\ d {3 })

    • | - or
    • | - 要么

    • \d{1,15} - 1 to 15 digits
    • \ d {1,15} - 1到15位数

  • (?:\ d {1,3}(?:\。\ d {3}){0,4} | \ d {1,15}) - 2个替代方案:\ d {1,3}(?:\ 。\ d {3}){0,4} - 1到3位数,后跟0到4个点(\。)和3位数(\ d {3})| - 或\ d {1,15} - 1到15位数

  • (?:,\d{1,6})? - optional sequence (1 or 0 occurrences) of a comma followed with 1 to 6 digits
  • (?:\ d {1,6})? - 逗号的可选序列(1或0次出现),后跟1到6位数

  • $ - end of string
  • $ - 结束字符串

#2


3  

Just throwing this out as an alternative. In my opinion you shouldn't use a Regex in this case. Why? Because the Regex that will actually match the question won't be easy to understand.

只是把它作为一种替代方案。在我看来,在这种情况下你不应该使用正则表达式。为什么?因为实际上匹配问题的正则表达式不容易理解。

Much simpler solutions exist for the problem. Take for example this:

该问题存在许多更简单的解决方案。以此为例:

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 fancy. But it works, unlike the other solutions provided until now.

没有什么花哨。但它的工作原理与迄今为止提供的其他解决方案不同。

But what about different culture settings? Different cultures use different thousand separators. That's where something like Double.TryParse can come in:

但是不同的文化背景呢?不同的文化使用不同的千分离器。这就是Double.TryParse之类的东西可以进来:

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. Set it to en-US and it will fail.

以上是有效的,因为nl-NL文化使用了这种格式。将其设置为en-US,它将失败。

Neither really use a Regex to get the job done, but they can still produce the desired result.

既没有真正使用正则表达式完成工作,但他们仍然可以产生所需的结果。

#3


0  

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

试试这个:^ [\ d \。\,] {1,15}([\。\,] \ d {0,6})?$

#1


2  

I think you need

我想你需要

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

See the regex demo

请参阅正则表达式演示

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

整数部分仅允许1到15位数字(或5组3位数字块,用。digit分组符号分隔),并且可选地后跟逗号和1到6个十进制数字。

  • ^ - start of string
  • ^ - 字符串的开头

  • (?:\d{1,3}(?:\.\d{3}){0,4}|\d{1,15}) - 2 alternatives:
    • \d{1,3}(?:\.\d{3}){0,4} - 1 to 3 digits, followed with 0 to 4 sequences of a dot (\.) and 3 digits (\d{3})
    • \ d {1,3}(?:\。\ d {3}){0,4} - 1到3位数字,后跟0到4个点(\。)和3位数字(\ d {3 })

    • | - or
    • | - 要么

    • \d{1,15} - 1 to 15 digits
    • \ d {1,15} - 1到15位数

  • (?:\ d {1,3}(?:\。\ d {3}){0,4} | \ d {1,15}) - 2个替代方案:\ d {1,3}(?:\ 。\ d {3}){0,4} - 1到3位数,后跟0到4个点(\。)和3位数(\ d {3})| - 或\ d {1,15} - 1到15位数

  • (?:,\d{1,6})? - optional sequence (1 or 0 occurrences) of a comma followed with 1 to 6 digits
  • (?:\ d {1,6})? - 逗号的可选序列(1或0次出现),后跟1到6位数

  • $ - end of string
  • $ - 结束字符串

#2


3  

Just throwing this out as an alternative. In my opinion you shouldn't use a Regex in this case. Why? Because the Regex that will actually match the question won't be easy to understand.

只是把它作为一种替代方案。在我看来,在这种情况下你不应该使用正则表达式。为什么?因为实际上匹配问题的正则表达式不容易理解。

Much simpler solutions exist for the problem. Take for example this:

该问题存在许多更简单的解决方案。以此为例:

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 fancy. But it works, unlike the other solutions provided until now.

没有什么花哨。但它的工作原理与迄今为止提供的其他解决方案不同。

But what about different culture settings? Different cultures use different thousand separators. That's where something like Double.TryParse can come in:

但是不同的文化背景呢?不同的文化使用不同的千分离器。这就是Double.TryParse之类的东西可以进来:

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. Set it to en-US and it will fail.

以上是有效的,因为nl-NL文化使用了这种格式。将其设置为en-US,它将失败。

Neither really use a Regex to get the job done, but they can still produce the desired result.

既没有真正使用正则表达式完成工作,但他们仍然可以产生所需的结果。

#3


0  

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

试试这个:^ [\ d \。\,] {1,15}([\。\,] \ d {0,6})?$