Home > Mobile >  Need Regex to validate Numeric values and empty spaces
Need Regex to validate Numeric values and empty spaces

Time:12-11

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?

CodePudding user response:

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
  • Related