3

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.

2
  • 1
    require 'csv'; CSV.parse(mystring) and you don't need the troublesome regex. Commented Nov 22, 2011 at 16:13
  • rubular.com is an excellent place to tinker with your expression. Commented Nov 22, 2011 at 16:16

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.