0

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. [[1.32, 42.14],[1.32, 42.124]] //2d array with only 2 floating values in each 1d arrays

  2. [[[1.32, 42.14],[1.32, 42.124]],[[1.32, 42.14],[1.32, 42.124]]] // an array of the 2d array above #1

  3. [[[[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:

  1. does not accept spaces in between array values. e.g. accepts [1.32,42.124] but [1.32, 42.124] is erroneous.

  2. 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]].

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

Christopher
  • 540
  • 2
  • 6
  • 33
  • 1
    Don't do this in regex... it's recursive and regex is the worst thing to handle it. – Mike Guelfi Jan 24 '20 at 04:37
  • @MikeGuelfi What do you suggest sir? – Christopher Jan 24 '20 at 04:41
  • This has a strong relation to your problem https://stackoverflow.com/questions/17140850/how-to-parse-a-string-and-return-a-nested-array – mrEvgenX Jan 24 '20 at 04:46
  • There's no indication of the root problem, but the answer isn't regex. If as @mrEvgenX suspects it's validating the input his suggestion is good. But if you're expecting valid input anyway, you could collect it directly into an array? – Mike Guelfi Jan 24 '20 at 04:57

0 Answers0