gpt4 book ai didi

regex - 如何计算 Rust 中的正则表达式匹配?

转载 作者:行者123 更新时间:2023-11-29 07:57:08 28 4
gpt4 key购买 nike

我想用 Rust 计算字符串中正则表达式的匹配项。我设法打印了所有匹配项:

let re = Regex::new(r"(?i)foo").unwrap();
let result = re.find_iter("This is foo and FOO foo as well as FoO.");
for i in result {
println!("{}", i.as_str())
}

但我无法简单地获取匹配项的数量。我找不到任何能给我计数的函数。我还尝试了 size_hint(),但效果不佳。我有什么办法可以做到这一点吗?

Here是我正在寻找的 Scala 版本。

最佳答案

您已经有了迭代器,所以只需计算迭代器中元素的数量:

re.find_iter("This is foo and FOO foo as well as FoO.").count()

关于regex - 如何计算 Rust 中的正则表达式匹配?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57741244/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com