Home > Software engineering >  How to exclude a regex match when I have comma without space after
How to exclude a regex match when I have comma without space after

Time:11-17

I have a regex working with 99% of my situations. But one is not working

My input is 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, XXXXX: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]

This regex allows to match all key:value in between the first [ and last ]

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

It split all key:value when a comma is detected, but I have specific data where I have a comma without space after. For example, the last date of my example is split because of , I search to exclude this split match to match all 2022-09-17,XXXXX:1

I search for a regex that match only data that a separate by , and not ,

Here is the example with the split of the last data I search to prevent https://regex101.com/r/8fd7Xv/1

CodePudding user response:

You can add a space, or 1 or more spaces at the positions that you assert a comma.

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

See the updated pattern https://regex101.com/r/Dlx8Xi/1

  • Related