[英]How to count regex matches in Rust?
我想用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()
,但這也行不通。 我能以任何方式做到這一點嗎?
這是我正在尋找的Scala版本。
你已經有了迭代器,所以只計算迭代器中的元素數量:
re.find_iter("This is foo and FOO foo as well as FoO.").count()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.