From: Dan Zwell Date: 2007-08-29T08:16:12+09:00 Subject: Re: gsub help 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 > > > Hurcan, That's a very hard problem. If you think creatively, you can probably come up with a decent algorithm, and then a handful of examples that break it. For each "=" or "==", you need to decide whether it is between a pair of unescaped slashes, quotes, or other form of quoting (like %Q|| or %x{}). I doubt there is any way to do this with a regexp. You need to write some code that makes decisions about whether any of the following symbols has semantic meaning: |\{}@^#() and any other that can be used in the % quoting construct Once you determine which ones have meaning (and which are escaped, or insignificant in their current context), you can decide whether some "=" should be re-spaced, based on its position relative to these characters. The more I think about it, the more I think you can't do this without writing a ruby parser. puts ("this problem is nontrivial" == true) puts %q("this problem is nontrivial" == true) puts %q("this problem is nontrivial", says a string == true) Good luck, Dan