E4181#
Compiler diagnostic name: named_capture_in_regex_literal_used_in_lex_is_not_supported.
lexmatch 或 lexscan 使用的正则字面量不支持具名捕获组。
lexmatch 和 lexscan 使用 MoonBit 的 as 语法绑定匹配文本。正则本身内部的具名捕获组不属于这种绑定模型。
错误示例#
///|
pub fn match_text(text : String) -> Unit {
lexmatch text with longest {
re"^(?<word>abc)$" => ()
_ => ()
}
}
该正则包含命名捕获组 (?<word>...)。
修改建议#
使用普通正则分组,并通过 as 绑定匹配文本。
///|
pub fn match_text(text : String) -> Unit {
lexmatch text with longest {
(re"^(abc)$" as word) => ignore(word)
_ => ()
}
}