Reputation: 12215
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
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
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 ]"]
Upvotes: 2