I'm porting a pluralizer to Rust and I'm having some difficulty with regular expressions. I can't get the Regex::replace()
method to replace a numbered capture group as I would expect. For example, the following displays an empty string:
let re = Regex::new("(m|l)ouse").unwrap();
println!("{}", re.replace("mouse", "$1ice"));
I would expect it to print "mice", as it does in JavaScript (or Swift, Python, C# or Go)
var re = RegExp("(m|l)ouse")
console.log("mouse".replace(re, "$1ice"))
Is there some method I should be using instead of Regex::replace()
?
Examining the Inflector crate, I see that it extracts the first capture group and then appends the suffix to the captured text:
if let Some(c) = rule.captures(&non_plural_string) {
if let Some(c) = c.get(1) {
return format!("{}{}", c.as_str(), replace);
}
}
However, given that replace
works in every other language I've used regular expressions in, I would expect it work in Rust as well.