什么是“最好的"?美国货币正则表达式? [英] What is "The Best" U.S. Currency RegEx?

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

问题描述

快速搜索货币正则表达式会显示 很多结果.

我在选择一个时遇到的问题是,如果不测试所有边缘情况,则很难验证正则表达式.有没有人有经过彻底测试的美元正则表达式?

我唯一的要求是匹配的字符串是美国货币并解析为System.Decimal:

<前>[ws][sign][digits,]digits[.fractional-digits][ws]方括号([ 和 ])中的元素是可选的.下表描述了每个元素.元素描述ws 可选的空格.标志 可选标志.数字 从 0 到 9 的数字序列., 特定于文化的千位分隔符..特定于文化的小数点符号.小数位数 范围从 0 到 9 的数字序列.

解决方案

这里有一些来自 Regex Buddy 制造商的东西.这些来自图书馆,所以我相信它们已经过彻底测试.

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

匹配;杰格软件:^[+-]?[0-9]{1,3}(?:,?[0-9]{3})*.[0-9]{2}$

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

匹配;杰格软件:^[+-]?[0-9]{1,3}(?:,?[0-9]{3})*(?:.[0-9]{2})?$

数字:货币金额 US &欧盟(美分可选)可以使用美式 123,456.78 符号和欧式 123.456,78 符号.可选的千位分隔符;可选的两位小数

匹配;杰格软件:^[+-]?[0-9]{1,3}(?:[0-9]*(?:[.,][0-9]{2})?|(?:,[0-9]{3})*(?:.[0-9]{2})?|(?:.[0-9]{3})*(?:,[0-9]{2})?)$

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?

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. 

解决方案

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})?$

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})?)$

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

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