re = /(((?:[+\-\/*]6+|6+[+\-\/*]|([^6]|\b)6{1,2}(?3)|(?3)6{6,}(?3)|[0-57-9]6+|6+[0-57-9])(*SKIP)(*F))|6)/m
str = '66
老铁666这类的, 6666
三到五个六66666
5666有其他数字不替换
6665
566665
666666
66
+66
666-
'
subst = '六'
result = str.gsub(re, subst)
# Print the result of the substitution
puts result
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Ruby, please visit: http://ruby-doc.org/core-2.2.0/Regexp.html