Home > Back-end >  How to match all characters between [ ] and except ", "
How to match all characters between [ ] and except ", "

Time:11-14

Hello I try to extract each group of data ( each data is separated by , from a string like that

MyString=[XXXXXX:XX   XX   XX   XX, XXXXX:332.83, XXXXX:XXX-XX-XX XX:XX:XX, XXXX:0.0, XXXX:2, XXXX:0, XXXX:-256, counter_tipeee:5, XXXX:136935, XXXX:0, XXXX:XX XXX XXX, XXXX:0.5, XXXXX:true, XXXX:0.509375, XXX:0.0, XXXX:[2022-06-14 06:45:00], 2022-09-17 XXXXX:1]

With this regex, I can match all characters except ,

([^,]*)

https://regex101.com/r/lCN2YK/1

But I search to mismatch ,

The problem is if I remove space with \s, it removes space from certain data of my string. I search to extract all data that is not precisely coma space , Another problem with my regex, it does not exclude the first [ and the last ] from my string. I can't exclude all [ ] because certain data have [ ]

I found this regex to exclude the first and last character ^.(.*).$ but don't know how to combine my two regex

https://regex101.com/r/CAsKHE/1

The output that I expect is

List<String> My_goal=  [

XXXXXX:XX   XX   XX   XX
XXXXX:332.83
XXXXX:XXX-XX-XX XX:XX:XX
XXXX:0.0, XXXX:2

....

2022-09-17 XXXXX:1
]

CodePudding user response:

Try this:

(?<=(?<!: *)\[).*?(?=,)|(?<=, *(?=[^ \r\n]))(?:.*?(?=,)|[^,\r\n\[\]] ?(?=\])|[^,\r\n] \](?= *\]))

See regex demo.

  • Related