Robert
Robert

Reputation: 4406

What is the regular expression to match any decimal in C#?

How would you write a regular expression to match this C# Operator. I would like the user to be able to refine the amount as much as the language will allow.

Ex:

21589.69 1.45 385.4681

These are random examples I get from their data.

I tried:

\d+(?:,\d{1,2})?
\d(\.\d{1,3})?

These don't allow the level I need

I am trying to limit an MVC text box using the REGEX data annotation:

[RegularExpression(@"\d{1}(\.\d{1,3})?", ErrorMessage = "Must be valid number")]

Upvotes: 0

Views: 47

Answers (1)

stuartd
stuartd

Reputation: 73243

regular-expressions.info suggests ^[-+]?[0-9]*\.?[0-9]+$ to match decimals, or ^[-+]?[0-9]*\.?[0-9]+(?:[eE][-+]?[0-9]+)?$ to also handle scientific notation.

Upvotes: 1

Related Questions