什么是“最佳”?美国货币RegEx? [英] What is "The Best" U.S. Currency RegEx?

查看:73
本文介绍了什么是“最佳”?美国货币RegEx?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

快速搜索货币正则表达式会弹出有很多结果

A quick search for currency regex brings up a lot of results.

我选择一个问题是正则表达式很难在不测试所有边缘情况的情况下进行验证。是否有人有经过全面测试的美元正则表达式?

The problem I have in choosing one is that regex is difficult to verify without testing all the edge cases. Does anyone have a regex for U.S. currency that has been thoroughly tested?

我唯一的要求是匹配的字符串是美元,并解析为 System.Decimal

My only requirement is that the matched string is U.S. currency and parses to System.Decimal:


[ws][sign][digits,]digits[.fractional-digits][ws] 

Elements in square brackets ([ and ]) are optional. 
The following table describes each element. 

ELEMENT             DESCRIPTION
ws                  Optional white space.
sign                An optional sign.
digits              A sequence of digits ranging from 0 to 9.
,                   A culture-specific thousands separator symbol.
.                   A culture-specific decimal point symbol.
fractional-digits   A sequence of digits ranging from 0 to 9. 


推荐答案

Regex Buddy制造商的一些东西。这些来自图书馆,因此我相信它们已经过全面测试。

here's some stuff from the makers of Regex Buddy. These came from the library so i'm confident they have been thoroughly tested.

数字:货币金额(必须为美分)
可选的千位分隔符;强制性两位数

Number: Currency amount (cents mandatory) Optional thousands separators; mandatory two-digit fraction

Match; JGsoft:
^[+-]?[0-9]{1,3}(?:,?[0-9]{3})*\.[0-9]{2}$

数字:货币金额(美分可选)
可选千位分隔符;可选的两位小数

Number: Currency amount (cents optional) Optional thousands separators; optional two-digit fraction

Match; JGsoft:
^[+-]?[0-9]{1,3}(?:,?[0-9]{3})*(?:\.[0-9]{2})?$

数字:货币金额US&欧盟(可选的美分)
可以使用美国风格的123,456.78表示法和欧洲风格的123.456,78表示法。可选的千位分隔符;可选的两位小数

Number: Currency amount US & EU (cents optional) Can use US-style 123,456.78 notation and European-style 123.456,78 notation. Optional thousands separators; optional two-digit fraction

Match; JGsoft:
^[+-]?[0-9]{1,3}(?:[0-9]*(?:[.,][0-9]{2})?|(?:,[0-9]{3})*(?:\.[0-9]{2})?|(?:\.[0-9]{3})*(?:,[0-9]{2})?)$

这篇关于什么是“最佳”?美国货币RegEx?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆