str = "x^2 + 12317 +X^2 - Length"
str = " x ^ 2 + y ^ 2 = 2 "
count = 8.4
print("count: #{count} ")
if /^(\s*[a-zA-Z]+\s*\(\s*[0-9a-zA-Z]+\s*\)\s*[\+\-\*\/\=]\s*)?\s*[0-9a-zA-Z]+\s*\^?\s*[0-9a-zA-Z]*\s*[\+\-\*\/]\s*(\s*[0-9a-zA-Z]+\^?[0-9a-zA-Z]*\s*)\s*(\s*[\+\-\*\/\=]\s*[0-9a-zA-Z]+)?/ =~ str
puts(MATCHED)
else
puts(DISMATCHED)
end
str = str.gsub(/\s+/, "")
str = str.gsub(/\+/, " + ")
str = str.gsub(/\-/, " - ")
str = str.gsub(/\*/, " * ")
str = str.gsub(/\//, " \\ ")
str = str.gsub(/\=/, " = ")
puts(str)
运行结果
count: 8.4 matched
x^2 + y^2 = 2