RegEx模式用于可选的千分位分隔符和不同小数点的求和。

huangapple go评论70阅读模式
英文:

RegEx pattern for sums with optional thousands separator and different decimal points

问题

(?<=[^\d,.-])[-]?\d{1,3}([ ,]?\d{3})*([,.]\d{2})?(?=[^\d,.-])
英文:

Sums in invoice are always two decimals after comma.
Decimal point may be comma or point.
Number may contain single space as thousands separator.
Optionally they numbers may start with - sign.

Example:

Subtotal,    1 000.00 EUR
VAT    1000,00
Total, sum:    _343 444.45 EUR

Results should be

1 000.00
1000,00
343 444.45

Which is C# .NET RexExp pattern for this.
There are lot of similar questions but havent found excatly same.
Using .NET 7 ASP.NET MVC.

Answer in https://stackoverflow.com/questions/24230290/regex-valid-numbers-with-thousand-separator assumes that every number is in sepearate line.

答案1

得分: 2

-?[0-9]{1,3}(?: ?[0-9]{3})*[.,][0-9]{2}

解释:

  • -? - 可选的 - 符号,
  • [0-9]{1,3} - 一到三位数字。第一个千位分隔符之前的数字块,
  • (?: ?[0-9]{3})* - 可选的空格和三位数字块,可以重复任意次,
  • [.,] - 点号或逗号:小数分隔符,
  • [0-9]{2} - 两位数字:小数部分。

演示 在此

英文:

-?[0-9]{1,3}(?: ?[0-9]{3})*[.,][0-9]{2}

Explanation:

  • -? - optional - sign,
  • [0-9]{1,3} - one to three digits. Block of digits before first thousands separator,
  • (?: ?[0-9]{3})* - optional space and block of three digit repeated any number of times,
  • [.,] - dot or comma: decimal separator,
  • [0-9]{2} - two digits: decimal part.

Demo here.

huangapple
  • 本文由 发表于 2023年5月18日 13:54:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76278087.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定