regexdartregex-lookarounds

Why does this regular expression fail in dart?


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

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

fails with

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

Why is that?

Dart SDK version: 3.5.0 (stable) (None) on "linux_x64"


Solution

  • Because dart uses the ECMAScript Regex Syntax where the conditional (?(?<=i:)(\d+)|()) is not supported.

    The fiddle you linked is set to use PRCE2 (PHP) Syntax. If you change that setting to ECMAScript, you will get an error ...