代码之家  ›  专栏  ›  技术社区  ›  jvndev

为什么这个正则表达式在dart中失败了?

  •  0
  • jvndev  · 技术社区  · 1 年前

    https://regex101.com/r/kIwSGq/2

    void main() {
      const inputRegex = r'^([a|i|r]):(?(?<=i:)(\d+)|()):(\d+)$';
      final RegExp regExp = new RegExp(inputRegex);
    }
    

    失败与

    FormatException: Invalid group ^([a|i|r]):(?(?<=i:)(\d+)|()):(\d+)$
    
    

    为什么?

    Dart SDK版本:“linux_x64”上的3.5.0(稳定)(无)

    1 回复  |  直到 1 年前
        1
  •  1
  •   derpirscher    1 年前

    因为dart使用ECMAScript正则表达式语法,其中条件 (?(?<=i:)(\d+)|()) 不支持。

    您链接的小提琴设置为使用PRCE2(PHP)语法。如果你将该设置更改为ECMAScript,你会得到一个错误。。。