我正在尝试使用以下正则表达式验证 excel 公式样式:
=SUM\(((?:\w+\d+)(?::\w+\d+)?)((?:,\w+\d+)(?::\w+\d+)?)*\)
在这个来源:
应该通过
=SUM(A1,A11:A212,A12:A56,A342:A12,A3)
=SUM(A11:A12,A12:a12,A34:A3)
=SUM(A1,A2,A3)
=SUM(A1)
应该失败
=SUM(A11:A212:A2,A12:A56,A4,A342:A12)
而且我的验证部分正在工作,但我不知道如何对每个逗号分隔值进行分组。他们应该是:
我希望它们如何分组:
=SUM(A1,A11:A12,A12:A56,A3) // Groups: $1 = A1 $2 = A11:A12 $3 = A12:A56 $4 = A3
=SUM(A11:A12,A10:A12,A34:A3) // Groups: $1 = A11:A12 $2 = A10:A12 $3 = A34:A3
=SUM(A1,A2,A3) //Groups: $1 = A1 $2 = A2 $3 = A3
=SUM(A1) //Groups: $1 = A1
他们目前的分组方式:
=SUM(A1,A11:A12,A12:A56,A3) // Groups: $1 = A1 $2 = A3
=SUM(A11:A12,A10:A12,A34:A3) // Groups: $1 = A11:A12 $2 = A34:A3
=SUM(A1,A2,A3) //Groups: $1 = A1 $2 = A3
=SUM(A1) //Groups: $1 = A1
注意,它对第一个和最后一个进行分组。我对 REGEX 很陌生,所以如果我在这里做一些糟糕的事情,请指出我正确的方向。谢谢!