我正在编写一个数学表达式检查器。我有这个字符串:
oper = "((234+3.32)+(cos4-sin65))"
我想通过分隔所有“()”和运算符减去数字或三角比来拆分此字符串以获得此结果:
oper = ['(', '(', '234', '+', '3.32', ')', '+', '(', 'cos4', '-', 'sin65', ')', ')']
如何拆分?
最佳答案
ruby :
oper = "((234+3.32)+(cos4-sin65))"
re = Regexp.union("(" ,")", "+", "-", /[^()\-+]+/)
p oper.scan(re) # => ["(", "(", "234", "+", "3.32", ")", "+", "(", "cos4", "-", "sin65", ")", ")"]
关于python - 如何将 float 拆分并保持在一起 Ruby 和 Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53438118/