Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我正在尝试使用 Ptython 中的 RegEx 匹配来提取线性方程的 LHS 和 RHS。
exp="+1-3=x+2"; parts = re.search("(.*?)=(.*?)", exp); left = parts.group(1); right = parts.group(2);
尽管left正确捕获了 的值,但 的值为right空。
left
right
有什么我做错了吗?不用说我是 Python 新手。
谢谢。
通过删除问号使右侧贪心:
parts = re.search("(.*?)=(.*)", exp);
否则匹配在=.
=
或者,用于$锚定到字符串的末尾。
$
最后,值得注意的是,您实际上并不需要正则表达式:
left, _, right = exp.partition('=')