E4232

E4232#

编译器诊断名称:regex_constant_in_pattern。

正则表达式常量不能用作普通模式。在普通 match 中匹配常量,不等同于用正则表达式匹配文本或字节。

错误示例#

///|
pub const WORD = re"[a-z]+"

///|
pub fn matches(value : Regex) -> Bool {
  match value {
    WORD => true
    _ => false
  }
}

建议#

匹配文本时,请通过 =~、lexmatch 或 lexscan 使用正则表达式。不要使用普通常量模式比较正则表达式的值。

///|
pub const WORD = re"[a-z]+"

///|
pub fn matches(text : String) -> Bool {
  text =~ WORD
}

///|
test {
  assert_true(matches("hello"))
  assert_false(matches("123"))
}

对于字节正则表达式常量,使用其 execute 方法匹配字节。