I'm new to RegEx. I need to exactly match the given value to be used in location fencing. (problems are stated on the bottom-most part of thread). I will be using this on javascript and posibly in python. These are the formats that I must allow:
[[1.32, 42.14],[1.32, 42.124]]
//2d array with only 2 floating values in each 1d arrays[[[1.32, 42.14],[1.32, 42.124]],[[1.32, 42.14],[1.32, 42.124]]]
// an array of the 2d array above #1[[[[1.32, 42.14],[1.32, 42.124]],[[1.32, 42.14],[1.32, 42.124]]],[[[1.32, 42.14],[1.32, 42.124]],[[1.32, 42.14],[1.32, 42.124]]]]
// an array of the array above. #2
I managed to create these expressions below:
test 1 - Test for float array with only 2 values inside: e.g. [1.23,42.124]
RegEx:
(\[((([0-9]*)[.])?[0-9]+[,]?){2}(?<!,)\])
test 2 - Test for arrays of test 1: e.g. [[1.23,42.124],[1.23,42.124]]
RegEx:
(\[(((\[((([0-9]*)[.])?[0-9]+[,]?){2}(?<![,])\])[,]?)+)(?<![,])\])
test 3 - Test for array of test 2: e.g. [[[1.23,42.124],[1.23,42.124]],[[1.23,42.124],[1.23,42.124]]]
RegEx:
(\[((\[(((\[((([0-9]*)[.])?[0-9]+[,]?){2}(?<![,])\])[,]?)+)(?<![,])\])[,]?)+(?<![,])\])
test 4 - test for array of test 3: e.g. [[[[1.23,42.124],[1.23,42.124]],[[1.23,42.124],[1.23,42.124]]],[[[1.23,42.124],[1.23,42.124]],[[1.23,42.124],[1.23,42.124]]]]
RegEx:
(\[(((\[((\[(((\[((([0-9]*)[.])?[0-9]+[,]?){2}(?<![,])\])[,]?)+)(?<![,])\])[,]?)+(?<![,])\])[,]?)+)(?<![,])\])
So I completed the expression by combining test 2 to test 4 together in an |
(or):
Complete Regex:
/(\[(((\[((([0-9]*)[.])?[0-9]+[,]?){2}(?<![,])\])[,]?)+)(?<![,])\])|(\[((\[(((\[((([0-9]*)[.])?[0-9]+[,]?){2}(?<![,])\])[,]?)+)(?<![,])\])[,]?)+(?<![,])\])|(\[(((\[((\[(((\[((([0-9]*)[.])?[0-9]+[,]?){2}(?<![,])\])[,]?)+)(?<![,])\])[,]?)+(?<![,])\])[,]?)+)(?<![,])\])/g
These are my current problems with this expression:
does not accept spaces in between array values. e.g. accepts
[1.32,42.124]
but[1.32, 42.124]
is erroneous.I manage to mismatch it if it has trailing comma on last value
[[1.32,42.124],[1.32,42.124],]
. But it accepts succeeding values without commas e.g.[[1.32,42.124][1.32,42.124]]
.Last is it does not accept typical multi-line formatted json.
I need to accept spaces in between array values, and do not accept succeeding values without commas.