johnnycakes
johnnycakes

Reputation: 2450

Ruby Regex - Need to replace every occurrence of a character inside a regex match

Here's my string:

mystring = %Q{object1="this is, a testyay', asdkf'asfkd", object2="yo ho', ho"}

I am going to split mystring on commas, therefore I want to (temporarily) sub out the commas that lie in between the escaped quotes.

So, I need to match escaped quote + some characters + one or more commas + escaped quote and then gsub the commas in the matched string.

The regex for gsub I came up with is /(".*?),(.*?")/, and I used it like so: newstring = mystring.gsub(/(".*?),(.*?")/ , "\\1|TEMPSUBSTITUTESTRING|\\2"), but this only replaces the first comma it finds between the escaped quotes.

How can I make it replace all the commas?

Thanks.

Upvotes: 3

Views: 952

Answers (2)

Mark Wilkins
Mark Wilkins

Reputation: 41262

I believe this is one way to achieve the results you are wanting.

newstring = mystring.gsub(/".*?,.*?"/) {|s|  s.gsub( ",", "|TEMPSUBSTITUTESTRING|" ) }

It passes the matched string (the quoted part) to the code block which then replaces all of the occurrences of the comma. The initial regex could probably be /".*?"/, but it would likely be less efficient since the code block would be invoked for each quoted string even if it did not have a comma.

Upvotes: 2

Alan Moore
Alan Moore

Reputation: 75272

Don't bother with all that, just split mystring on this regex:

,(?=(?:[^"]*"[^"]*")*[^"]*$)

The lookahead asserts that the comma is followed by an even number of quotes, meaning it's not inside a quoted value.

Upvotes: 1

Related Questions