E4181

E4181#

Compiler diagnostic name: named_capture_in_regex_literal_used_in_lex_is_not_supported.

lexmatch 使用的正则字面量不支持命名捕获组。

lexmatch 可以通过词法模式中的位置绑定器绑定正则模式的部分内容。正则本身内部的命名捕获组不属于受支持的词法模式绑定模型,因此会被拒绝。

错误示例#

///|
pub fn match_text(text : String) -> Unit {
  lexmatch text with longest {
    ("(?<word>abc)", _) => ()
    _ => ()
  }
}

该正则包含命名捕获组 (?<word>...)

修改建议#

使用 lexscan 时,请使用普通正则分组,并在需要时通过 after= 绑定匹配到的后缀。

///|
pub fn match_text(text : String) -> Unit {
  lexscan text with longest {
    (re"^abc", after=_) => ()
    _ => ()
  }
}