Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON numbers Regular Expression

Tags:

json

regex

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!

like image 378
Joe Crawley Avatar asked Nov 12 '12 08:11

Joe Crawley


People also ask

Can JSON contain numbers?

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.

Can regex be used for numbers?

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.

Can you use regex in JSON?

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 \\ .

Can JSON have integer values?

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.


1 Answers

For reference, here's the "number" diagram from http://www.json.org/fatfree.html:

JSON number

The regex that should match this is:

-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+-]?\d+)?
like image 88
slackwing Avatar answered Sep 24 '22 22:09

slackwing