tags:

views:

63

answers:

3

This is my string:

msgid """We couldn't set up that account, sorry.  Please try again, or contact an ""admin (link is above)."

I want to remove all the double quotes except the first and last one. How may I do that?

+1  A: 

Here is an option as long as you know you always want a quote at the beginning and at the end.

Assume x is holding the sting you want to manipulate.

x = '"' + x.gsub('"', '') + '"'
Tony Eichelberger
The way I understood the question, he wanted `msgid "...."`, not `"msgid ..."`.
Michael Kohl
+1  A: 

This is assuming your strings will always be of the "msgid..." format shown above and your inteded output was 'msgid "text here"':

>> str.gsub(/(msgid )"{1,}(.*) "{1,}(.*)"/, '\1"\2 \3"')
=> "msgid "We couldn't set up that account, sorry.  Please try again or contact an admin (link is above).""
>> puts str.gsub(/(msgid )"{1,}(.*) "{1,}(.*)"/, '\1"\2 \3"')
msgid "We couldn't set up that account, sorry.  Please try again or contact an admin (link is above)."
Michael Kohl
A: 

I'd go with something like this:

msg = %q{msgid """We couldn't set up that account, sorry.  Please try again, or contact an ""admin (link is above)."}
print 'msgid "' << msg.sub('msgid ', '').gsub('"', '') << '"'
# >> msgid "We couldn't set up that account, sorry.  Please try again, or contact an admin (link is above)."

If "msgid" precedes every line, then remove it, deal with the double-quotes by stripping them, then put back "msgid" and add the desired leading and trailing double-quotes again.

Greg