E4218

E4218#

Compiler diagnostic name: lexscan_pattern_not_anchored.

lexscan 正则模式没有在要求的位置锚定。

使用 longest 策略的模式必须从输入开头开始匹配。请用 ^ 锚定每个此类正则的开头。

错误示例#

///|
pub fn classify(input : String) -> String {
  lexscan input with longest {
    re"[a-z]+" => "word"
    _ => "other"
  }
}

建议#

在正则开头添加 ^。只有当 case 必须消耗整个输入时,才还需要添加 $

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