正则表达式拆分iif表达式

时间:2018-01-17 08:47:48

标签: javascript regex vbscript

我正在尝试测试一个能够将以下表达式分成3部分的正则表达式:

  • 测试

如果有多个iif嵌套,它应该给我多个匹配

我有一些模式:

iif(testExpression, trueExpression, falseExpression)

iif((@HasMinimunRegulatedCAR@==0),(([t219]>1.5) OR ([t219]<-0.5)),(([t223]>1.5) OR ([t223]<-0.5)))

iif((@HasMinimunRegulatedCAR@==1), iif((@MFIUsePAR30@==1), ([t224]>0.25), iif((@MFIUsePAR90@==1), ([t225]>0.25), (1==1))), iif((@MFIUsePAR30@==1), ([t220]>0.25), iif((@MFIUsePAR90@==1), ([t221]>0.25),(1==1))))

我正在使用这个表达式但是当我有多个iif嵌套

时它不起作用
(^(iif\()|[^,]+)

我正在运行我的测试: https://regex101.com/

预期输出应为

testExpression
trueExpression
falseExpression

(@HasMinimunRegulatedCAR@==0)
([t219]>1.5) OR ([t219]<-0.5)
([t223]>1.5) OR ([t223]<-0.5)

(@HasMinimunRegulatedCAR@==1)
iif((@MFIUsePAR30@==1), ([t224]>0.25), iif((@MFIUsePAR90@==1), ([t225]>0.25), (1==1)))
iif((@MFIUsePAR30@==1), ([t220]>0.25), iif((@MFIUsePAR90@==1), ([t221]>0.25),(1==1)))

1 个答案:

答案 0 :(得分:1)

如果.Net正则表达式是一个选项,您可以使用balancing groups来捕获iif

一个选项会将所有嵌套括号捕获到名为Exp的组中:

(?>
    (?:iif)?(?<Open>\((?<Start>))   # When seeing an open parenthesis, mark
                                    # a beginning of an expression. 
                                    # Use the stack Open to count our depth.
    |
    (?<-Open>(?<Exp-Start>)\))      # Close parenthesis. Capture an expression.
    |
    [^()\n,]                        # Match any regular character
    |
    (?<Exp-Start>),(?<Start>)       # On a comma, capture an expression, 
                                    # and start a new one.
)+?
(?(Open)(?!))

Working Example,切换到标签 此选项捕获的内容超出了您的要求,但它确实为您提供了完整解析方程式所需的完整数据。

另一种选择是仅捕获顶级部分:

iif\(
(?:
  (?<TopLevel>(?>
      (?<Open>\()     # Increase depth count using the Open stack.
      |
      (?<-Open>\))    # Decrease depth count.
      |
      [^()\n,]+       # Match any other boring character
      |
      (?(Open),|(?!)) # Match a comma, bun only if we're already inside parentheses.  
  )+)
  (?(Open)(?!))
  |
  ,\s*   # Match a comma between top-level expressions.
)+
\)

Working Example 在这里,组$TopLevel将有三次捕获。例如,在您的上一个示例中,它捕获:

捕获0:(@HasMinimunRegulatedCAR@==1)
捕获1:iif((@MFIUsePAR30@==1), ([t224]>0.25), iif((@MFIUsePAR90@==1), ([t225]>0.25), (1==1)))
捕获2:iif((@MFIUsePAR30@==1), ([t220]>0.25), iif((@MFIUsePAR90@==1), ([t221]>0.25),(1==1)))