From: Pit Capitain Date: 2004-01-20T08:36:49+09:00 Subject: Re: regex help You could try multiple steps: 1) extract string literals 2) change comment in remaining text 3) add string literals For example: def format str literal = true str.split( /("(?:[^"\\]|\\.)*")/ ).map do |s| ( literal = !literal ) ? s : s.gsub( /\*\//, "*\\" ) end.join end The regexp used in split extracts all string literals (it handles the "a\"b" notation, too). By enclosing the regexp in parentheses split will add the string literals in its output, too. So the result of split is an array with the 2nd, 4th, 6th, ... elements being the string literals and the 1st, 3rd, 5th, ... elements being the text outside of the literals. The map uses a binary switch to decide whether to perform the desired substitution or not (this is the first time I could use map_with_index). This ensures that the string literals are left unchanged. The last step is joining the array elements back into one string. I don't think this will handle all possible cases, but maybe it is a starting point. Regards, Pit