On 19.03.2008 11:21, ciapecki wrote:
> On 19 Mrz., 10:09, Xavier Noria <f... / hashref.com> wrote:
> 
>> My interpretation of what you need is:
>>
>>     input = '111|"aaaa" bbbbb|c'
>>
>>     fields = input.split('|')
>>     fields[1].gsub!('"', '""')
>>     fields[1] = %Q{"#{fields[1]}"}
>>
>>     output = fields.join('|')
>>
>> Depending on the input that may not be valid, for example if fields[1]
>> may contain a pipe. Anyway being a mal-formed input assumptions depend
>> on the actual data.
> 
> your solution works, and thanks for that,
> I am waiting though for a regexp solution,

Even 2 regexps:

irb(main):001:0> input = '111|"aaaa" bbbbb|c'
=> "111|\"aaaa\" bbbbb|c"
irb(main):002:0> input.gsub(/[^|]+/) {|m| m.gsub!(/"/,'""') ? 
'"'<<m<<'"' : m}
=> "111|\"\"\"aaaa\"\" bbbbb\"|c"
irb(main):003:0> puts input.gsub(/[^|]+/) {|m| m.gsub!(/"/,'""') ? 
'"'<<m<<'"' : m}
111|"""aaaa"" bbbbb"|c
=> nil

Cheers

	robert