开发者

Regular expression to detect whether input is a formatted number

Regular Expressions have always seemed like black magic to me and I have never been able to get my head around building them. I am now in need of a Reg Exp (for validation putsposes) that checks that the user enters a number according to the following rules.

  • no alpha char开发者_运维百科acters
  • can have decimal
  • can have commas for the thousands, but the commas must be correctly placed

Some examples of VALID values:

1.23
100
1,234
1234
1,234.56
0.56
1,234,567.89

INVALID values:

1.ab
1,2345.67
0,123.45
1.24,687


You can try the following expression

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

Explanation:

  • The part before the point consists of
    • either 1-3 digits followed by (one or more) comma plus three digits
    • or just digits (at least one)
  • If then follows a dot also some digits must follow.


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

This works for all of your examples of valid data, and will also accept decimals that start with a decimal point. (I.e. .61, .07, etc.)

I noticed that all of your examples of valid decimals (1.23, 1,234.56, and 1,234,567.89) had exactly two digits after the decimal point. I'm not sure if this is coincidence, or if you actually require exactly two digits after the decimal point. (I.e. maybe you're working with money values.) The regular expression as I've written it works for any number of digits after the decimal point. (I.e. 1.2345 and 1,234.56789 would be considered valid.) If you need there to be exactly two digits after the decimal point, change the end of the regular expression from +)?$ to {2})?$.


try to use this regex

^(\d{1,3}[,](\d{3}[,])*\d{3}(\.\d{1,3})?|\d{1,3}(\.\d+)?)$


I know you asked for a regex but I think it's much saner to just call double.TryParse() and consider your input acceptable if that method returns true.

double dummy;
var isValid=double.TryParse(text, out dummy);

It won't match your testcases exactly; the major difference being that it is very lenient with commas (so it will accept two of your INVALID inputs).

I'm not sure why you care, but if you really do want comma strictness you could do a preprocessing step where you only check the validity of comma placement and then call double.TryParse() only if the string passes the comma placement test. (If you want to be truly careful, you'll have to honor the CultureInfo so you can know what character is used for separators, and how many digits there are between separators, in the environment your program finds itself in)

Either approach results in code that is more "obviously right" than a regex. For example, you won't have to live with the fear that your regex left out some important case, like scientific notation.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜