Share via


How to write regular expression to allow decimal with maximum range and 2 decimal point

Question

Thursday, December 20, 2018 2:25 AM

Hi,

Need advise on writing a regex to accept input from range

0.00 to 9,999,999,999,999,999.99

Thanks,

All replies (5)

Thursday, December 20, 2018 2:27 AM

I would suggest indicating how input is being received e.g. NumericUpDown, TextBox etc. And why it needs to be regular expressions.

Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
VB Forums - moderator


Thursday, December 20, 2018 3:59 AM

Hi,

I am using web.mvc, and trying to apply the validation at model level.

e.g. 

 [RegularExpression(@"^\d{10}$", ErrorMessage = "10 Numeric Digits only")]
 public string CustCode{ get; set; }

Thanks,


Thursday, December 20, 2018 7:19 AM

Check this expression:

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

Spaces and commas are not allowed. The decimal part is optional and must contain two decimal places.


Friday, December 21, 2018 1:14 AM

Question(s) if I may ..

            string signedIntOptionalFraction = @"[-+]?(\d{1,10}(\.\d{2})?)";
            string OptionalExponent = @"([\d\s]*E[+-]?\d{1,3})?";
            string composite = signedIntOptionalFraction + OptionalExponent;

            zqr = new Regex(composite).Match(input);
            rqz = new Regex(composite).Match(input2);
            qrz = new Regex(composite).Match(input);
  1. Should there be a provision for sign, or are the numbers this regex will encounter known to be positive ?

  2. which notation format will the numbers exhibit  (conventional: 3.14159; scientific  0.278128 E1) ?

  3. Is it necessary to match the two anchors (^ $) ?

My string

            string RGXstg = @"^(0*[1-9]\d{0,15}|0+)(\.\d\d)?$";
            string MyRGXstg = @"[-+]?(\d{1,10}(\.\d{2})?)";
            string input = "    2.7818281828    ";
            string MyRGXstg2 = @"[-+]?(\d{1,10}(\.\d{2})?)([\d\s]*E[+-]?\d{1,3})?";
            string input2 = "    2.7818281828 E-27   ";

            Match zlo = new Regex(RGXstg).Match(input);
            Match zqr = new Regex(MyRGXstg).Match(input);
            Match rqz = new Regex(MyRGXstg2).Match(input2);
            Match qrz = new Regex(MyRGXstg).Match(input);

is very similar to viorel's with only minor changes.

Also, one may have an interest in arranging the strings as follows (I hate long, complicated Regex strings)

            string signedIntOptionalFraction = @"[-+]?(\d{1,10}(\.\d{2})?)";
            string OptionalExponent = @"([\d\s]*E[+-]?\d{1,3})?";
            string composite = signedIntOptionalFraction + OptionalExponent;

            zqr = new Regex(composite).Match(input);
            rqz = new Regex(composite).Match(input2);
            qrz = new Regex(composite).Match(input);

Friday, December 21, 2018 8:39 AM

Hi mdcw,

Thank you for posting here.

For your question, you want to write regular expression to allow decimal with maximum range and 2 decimal point.

Please try this Regex expression ^(([0-9999999999999999]{1}\d*)|(0{1}))(\\d{2})$.

If you want to use regular expression in mvc, please post in the following forum.

https://forums.asp.net/1146.aspx/

Best regards,

Jack J Jun.

MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact [email protected].