E4183

E4183#

Compiler diagnostic name: regex_interpolation_is_not_supported.

Regex interpolation is not supported in lexmatch patterns.

lexmatch patterns are compiled as part of the match. They must be known as literal regex patterns instead of being built with string or regex interpolation.

Erroneous example#

///|
pub fn match_text(text : String, part : String) -> Unit {
  lexmatch text with longest {
    ("\{part}", _) => ()
    _ => ()
  }
}

The regex pattern tries to interpolate part.

Suggestion#

Use a literal regex pattern with lexscan. For dynamic regex matching, build a regex value and use a regex match expression instead.

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