Python数学正则表达式 [英] Python Math Regex

查看:104
本文介绍了Python数学正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用Python创建一种编程语言,而基本语言所缺少的一个主要组件是解析数学方程式的能力.我具有解析数学的功能,但我需要能够检查输入是否为数学方程式.我正在寻找一个正则表达式来匹配3 * x ^(4 * y)+ 1-(7 * y * z/x)之类的东西.

I am creating a programming language in Python and one main component that I'm missing for the basic language is the ability to parse mathematical equations. I have the function to parse the math but I need to be able to check if the input is a math equation. I'm looking for a regex to match something like 3*x^(4*y)+1-(7*y*z/x).

推荐答案

您不能用正则表达式来做您想做的事,因为它不是正则语言. Python具有一些扩展,您可能会滥用这些扩展来执行您想要的操作,但是它将无法维护,并且不会因使用解析器而获得任何收益.

You can't do what you want with regular expressions, it isn't a regular language. Python has extensions that you could abuse to do what you want but it would be un-maintainable and would not gain you anything over using a parser.

您想要的是一个解析器,一个非常易于使用的Python库称为 pyparsing .

What you want is a Parser, a great easy to use library for Python is called pyparsing.

这是一个与相关的答案,并提供了一个pyparsing示例 a>.

这篇关于Python数学正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆