E4220

E4220#

Compiler diagnostic name: regex_match_binding_not_supported.

A lexical case uses a binding that is not available in its matching mode.

For String and StringView inputs, first-match lexmatch supports both before= and after=. Longest-match mode starts at the beginning of the input, so it supports after= but not before=. Streaming lexscan consumes its input buffer and does not expose unmatched prefixes or suffixes through these bindings.

Erroneous example#

///|
pub fn first_word(input : String) -> StringView {
  lexmatch input with longest {
    (re"^[a-z]+" as word, before=_) => word
    _ => ""
  }
}

Suggestion#

Remove before=, anchor the regex at ^, and use after= when the unmatched suffix is needed.

///|
pub fn first_word(input : String) -> StringView {
  lexmatch input with longest {
    (re"^[a-z]+" as word, after=_) => word
    _ => ""
  }
}