E4182

E4182#

Compiler diagnostic name: regex_constant_ref_in_lex_is_not_supported.

Regex constants are not supported in this lexmatch context.

Legacy lexmatch can use regex constants only in limited contexts: a StringView target with the default first-match strategy. Other lexmatch forms, such as with longest, require literal regex patterns.

Warning

lexmatch is deprecated for new code. Prefer regex match expressions such as value =~ SOME_REGEX unless you specifically need legacy lexer-style behavior.

Erroneous example#

The following example uses a regex constant in a longest-match lexmatch:

///|
const WORD = re"[A-Za-z]+"

///|
pub fn match_word(input : String) -> Unit {
  lexmatch input with longest {
    (WORD, _) => ()
    _ => ()
  }
}

MoonBit will report an error.

Suggestion#

Use a literal regex pattern in that lexmatch case, or rewrite the code with a regex match expression:

///|
pub fn match_word(input : String) -> Unit {
  lexmatch input with longest {
    ("[A-Za-z]+", _) => ()
    _ => ()
  }
}