Home > Enterprise >  Regex for matching arrays of values or another arrays
Regex for matching arrays of values or another arrays

Time:09-22

I'm trying to create a regex which will capture whole array of any objects inside it.

I've got example input string:

[2020-05-29T10:00:00, 12.5, 'Test text'][][[], ['Some Data']][['String with[ \'escaped quote][ and parenthesis inside it']]

Expected matches are:

Match 1: [2020-05-29T10:00:00, 12.5, 'Test text']
Match 2: []
Match 3: [[], ['Some Data']]
Match 4: [['String with[ \'escaped quote][ and parenthesis inside it']] // If this one is possible it's brilliant

Regex which I've already created is: \[[a-zA-Z0-9\-,' :\.\[]*\], but it doesn't handle array of arrays and parenthesis inside strings.

I would be really grateful for you help!

CodePudding user response:

This is similar to the question Regex nested parentheses - you should look at the accepted answer for a great explanation of what's going on.

The regex you want is, I believe:

\[(?>'(?:[^'\\]|\\.)*'|\[(?<DEPTH>)|\](?<-DEPTH>)|'(?:[^'\\]|\\.)*'|[^\[\]] )*\](?(DEPTH)(?!))
  • Related