python - 正则表达式避免不必要的组

标签 python regex

我有这个代码:

string = """a = 10 + 15
b = 50 + b
c = a + b
d = c + 50"""


letter = "([a-z])"
signs = "(\+|\-|\*|\/)"
regex = re.compile(r"{0} = (\d+) {1} (\d+)|"
                   r"{0} = (\d+) {1} {0}|"
                   r"{0} = {0} {1} {0}|"
                   r"{0} = {0} {1} (\d+)".format(letter, signs))signs))(\d+)".format(letter,signs))

如果我执行 re.search(regex,string).groups() 我最终会得到

('a', '10', '+', '15', None, None, None, None, None, None, None, None, None, None, None, None)
(None, None, None, None, 'b', '50', '+', 'b', None, None, None, None, None, None, None, None)
(None, None, None, None, None, None, None, None, 'c', 'a', '+', 'b', None, None, None, None)
(None, None, None, None, None, None, None, None, None, None, None, None, 'd', 'c', '+', '50')

但我只想要 4 个组。 [变量,val1,运算符,val2]

我正在使用列表理解

[r for r in re.search(regex,string).groups() if r != None]

但我想知道是否有办法在正则表达式本身中做到这一点。

最佳答案

在这种情况下,最好将正则表达式从四个单独的语句简化为一个稍微重载的语句,这确实需要修改字母:

letter = "[a-z]"
signs = "(\+|\-|\*|\/)"
regex = re.compile(r"({0}) = (\d+|{0}) {1} (\d+|{0})".format(letter, signs))signs))(\d+)".format(letter,signs))

关于python - 正则表达式避免不必要的组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24757323/

相关文章:

regex - 在 BASH 中使用正则表达式提取数字

python - 进行反向传播和梯度下降时,numpy 数组和矩阵出现问题

python - 在 python 中将整数数组转换为颜色字符串或颜色值的快速方法

python - 如何查找附加到一个或另一个类的元素? Selenium python

python - 获取视频上传的确切时间

regex - sed 或 perl 仅当第一个内部单词匹配时才删除外部括号

基于 C# 正则表达式的日期解析和提取

python - 在 Python 中删除前导/结尾和内部多个空格但不删除制表符、换行符或返回字符

javascript - 用于检查重复空格字符的正则表达式

python - 将变量指定为以正则表达式运算符 '^' 开头的值