简体   繁体   English

如何在Rust中计算正则表达式匹配?

[英]How to count regex matches in Rust?

I'd like to count the matches of a regex in a string with Rust. 我想用Rust的字符串计算正则表达式的匹配。 I managed to print all matches: 我设法打印所有比赛:

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())
}

But I fail to simply get the number of matches. 但我不能简单地得到比赛的数量。 I can't find any function that gives me the count. 我找不到能给我数数的任何功能。 I also tried size_hint() , but that does not work as well. 我也尝试过size_hint() ,但这也行不通。 Any way I can do that? 我能以任何方式做到这一点吗?

Here is the Scala version of what I'm looking for. 是我正在寻找的Scala版本。

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM