我正在将一个复数器移植到Rust,我对正则表达式有一些困难.我无法Regex::replace()像我期望的那样获得替换编号捕获组的方法.例如,以下显示空字符串:
let re = Regex::new("(m|l)ouse").unwrap();
println!("{}", re.replace("mouse", "$1ice"));
Run Code Online (Sandbox Code Playgroud)
我希望它可以打印"鼠标",就像在JavaScript(或Swift,Python,C#或Go)中一样
var re = RegExp("(m|l)ouse")
console.log("mouse".replace(re, "$1ice"))
Run Code Online (Sandbox Code Playgroud)
我应该使用Regex::replace()哪种方法而不是?
检查Inflector包,我看到它提取第一个捕获组,然后将后缀附加到捕获的文本:
if let Some(c) = rule.captures(&non_plural_string) {
if let Some(c) = c.get(1) {
return format!("{}{}", c.as_str(), replace);
}
}
Run Code Online (Sandbox Code Playgroud)
但是,鉴于replace我使用其他语言,我使用正则表达式,我希望它也适用于Rust.