英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论