I'm trying to write a regular expression for strings that are numbers in JSON. I'm still new to writing Regular expressions, I found a diagram of a machine for JSON numbers here , but I'm not sure how to attack it.
Here are some strings that should be found by the regex. "22", "55.75466", "-44.565" "55e-2" "69234.2423432 E78" Any help is appreciated!
JSON can actually take the form of any data type that is valid for inclusion inside JSON, not just arrays or objects. So for example, a single string or number would be valid JSON.
The regex [0-9] matches single-digit numbers 0 to 9. [1-9][0-9] matches double-digit numbers 10 to 99. That's the easy part. Matching the three-digit numbers is a little more complicated, since we need to exclude numbers 256 through 999.
json . You can write your regex inside quotation marks after the "Regular expressions" : . Note that in JSON format, the \ is a special character called escape character. So you will have to write a double backslash \\ .
The integer type is used for integral numbers. JSON does not have distinct types for integers and floating-point values. Therefore, the presence or absence of a decimal point is not enough to distinguish between integers and non-integers. For example, 1 and 1.0 are two ways to represent the same value in JSON.
For reference, here's the "number" diagram from http://www.json.org/fatfree.html:
The regex that should match this is:
-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+-]?\d+)?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With