Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Need Regex to validate Numeric values and empty spaces

I am using the below regex pattern to validate numerics and spaces

 "^[0-9\s]{1,50}" 

Attaching sample payload which i am receiving

 {"numberval":""}
 {"numberval":" "}
 {"numberval":"10,11,12"}

I am using python re module and compile method to validate the regex.
can any one help me with regex which can validate both the inputs?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Assuming you consider valid whitespace only or comma separated numbers, you could use this regex pattern:

^(?:\s*|\d+(?:,\s*\d+)*)$

Demo

This regex pattern says to match:

  • ^ from the start of the value
  • (?:
    • \s* either zero or more whitespace characters
    • | OR
    • \d+ a number
    • (?:,\s*\d+)* followed by zero or more other comma separated numbers
  • )
  • $ end of the value
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading