How do I get all the matches in repeat capture group? #1269
-
use regex::Regex;
fn main() {
let regex = Regex::new("((?<year>\\d{4})-(?<month>\\d{2})-(?<day>\\d{2}),?)+").unwrap();
for c in regex.captures_iter("2015-01-02,2025-12-24") {
println!("{c:?}");
for m in c.iter() {
println!("{m:?}");
}
}
} I'd expect to go over both "2015-01-02" and "2025-12-24", but it seems only the last group retains:
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
You can't. I'm not sure why you would expect that. Most regex engines don't let you access repeated matches of a capture group within one single overall match. The You need to change your regex pattern or use additional regexes. |
Beta Was this translation helpful? Give feedback.
-
Well do you mean you need an output matches of |
Beta Was this translation helpful? Give feedback.
You can't. I'm not sure why you would expect that. Most regex engines don't let you access repeated matches of a capture group within one single overall match. The
regex
crate does not allow it. It doesn't track the state necessary for such a thing.You need to change your regex pattern or use additional regexes.