From: Alpha Chen Date: 2007-12-04T06:25:00+09:00 Subject: Re: Postfix to Infix (#148) A pretty simplistic regex-based solution that doesn't minimize parentheses: #!/usr/bin/env ruby str = ARGV[0].split(/[^.\d+\-*\/]/).join(' ') while str !~ /^\(.*\)$/ str.sub!(/([^ ]+) ([^ ]+) ([+\-*\/])/, '(\1\3\2)') end puts str.gsub(/([+\-*\/])/, ' \1 ').sub(/^\((.*)\)$/, '\1')