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.
Answered by
BurntSushi
May 20, 2025
Replies: 1 comment
-
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.
0 replies
Answer selected by
BurntSushi
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.