I would like to know the regex pattern for negative decimals or whole numbers only. (e.g. -0, -0.0, -0.0000 or 4.1 are not allowed while -5.5, -4, -1 and -.022 are allowed). Can someone please think of a regex that matches this pattern?
/^-[^0]/
The above regex matches the negative sign at the front of the number, and not -0
. See if that doesn’t get you started.
I took a different path where I created two regex expressions (one for negative real numbers greater than -1 and the other for negative real numbers less than or equal to -1). How do you feel about my expressions below?
Negative real numbers (-1, 0):
pattern = “^(-0).{1}\d*[1-9]+\d*$”
Negative real numbers less than or equal to -1 (-inf, -1]:
pattern2 = “^-[1-9]\d*(.\d+)?$”
I plan to validate patterns for negative real numbers by using the search method
if(re.search(pattern, exp) or re.search(pattern2, exp)):
#body of code
TBH, Regex is not my strong suit, hence the unwillingness to go the distance, above, only to start you out. The best thing to do is find a Regex tester (there are many) and test your patterns repeatedly.