From: William James Date: 2007-08-29T10:15:17+09:00 Subject: Re: gsub help On Aug 28, 5:11 pm, hurcan solter wrote: > Hi there; > this is more of a regexp question but I'de be happy if you help me > since i am doing it in ruby. > i want to beautify some line by inserting or removing some space > around the operators like = or == > so i came up with an expression like; > > line.gsub!(/\s*([=]+)\s*/ ,' \1 ') > > But it applies every = on the line ofc, but I want to exclude > expressions between quotation marks ("sth=sth") or regexp (/sth=sth/). > I've tried negated character classes and such to no avail.How can I do > this? > Any help would be greatly appreciated... > Hurcan If " and / are the only wrappers you have to worry about, this may work. DATA.each{|line| line.chomp! array = line.scan( %r{ \G (?: [^"/]+ | " (?> \\. | [^"] ) * " | / (?> \\. | [^/] ) * / ) }xm ).map{|s| if s =~ %r{^[^"/]} s.gsub!( /\s*(=+)\s*/, ' \1 ' ) end s } puts array.join } __END__ /x==9/ { print "x==9 and AWK is everywhere!"; x=9 } /\/x==9/ { print "x==\"9"; x==9 }