Tobb
Tobb

Reputation: 12215

Gsub - regex: Replace all commas except ones between quotes or brackets

I'm trying to write a regex that identifies all commas, with some exceptions:

And substitutes them with some special character (for instance ¤) using gsub.

So for this example:

something=somethingElse, someThird="this is, a message with a comma!", someFourth=[ this, is, some, list ]

I would like the following result:

something=somethingElse¤ someThird="this is, a message with a comma!"¤ someFourth=[ this, is, some, list ]

I have found a few regexes that identifies these commas (like the in the answer below), but neither seem to work with gsub (they replace too much or nothing at all..)

Upvotes: 0

Views: 727

Answers (1)

anubhava
anubhava

Reputation: 785651

As long as quote and brackets are balanced and there is no escaped instances you can use this regex with lookahead:

/,(?=(([^"]*"){2})*[^"]*$)(?![^\[]*\])/g

RegEx Demo


Update: Here is working ruby code:

str = 'something=somethingElse, someThird="this is, a message with a comma!", someFourth=[ this, is, some, list ]';

print str.split(/\s*,(?=(?:(?:[^"]*"){2})*[^"]*$)(?![^\[]*\])\s*/);

Output:

["something=somethingElse", "someThird=\"this is, a message with a comma!\"", "someFourth=[ this, is, some, list ]"]

Online Code demo

Upvotes: 2

Related Questions