Parse commas not surrounded by brackets

2019-03-01 16:02发布

问题:

The input is a comma-separated list of fields.

Here is an example.

tna,performance,ma[performance,3],price

The issue is that some of the "fields" have parameters specified in square brackets and those parameters also have commas.

What RegEx could I use to break a string like that on commas, only when they are outside of brackets. I want the end result to be

tna
performance
ma[performance,3]
price

回答1:

This is what you need

(?<!\[[\w,]*?),

If brackets are nested within brackets, use this because the above would fail in that scenario..

(?<!\[[\w,]*?),(?![\w,]*?\])

works here



回答2:

Try this :

"[a-z0-9]*(\\[[a-z0-9\\[\\],]+\\])*"