python中的递归regex模式匹配

2024-04-19 14:25:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在python中匹配递归模式,但是我遗漏了一些东西,并且出现了错误。在

我希望实现:

var = [a-z]+
digit = [0-9]+
op=+,-,*,/
E->var|digit|op E E

例如:

^{pr2}$

这是我的代码:

import re
term="[a-z]+|[0-9]+"
op=[+-/*]
exp="("+term+"|("+op+" "+term+" "+term+")|(?R))

播种当我做re.match(exp,"+ x 1")时,我得到:

"sre_constants.error: unexpected end of pattern"

有人能帮我解决这个问题吗?在


Tags: 代码importrevarmatch错误模式error