Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

How do I get the name for each capture group when using Captures::iter? #1268

Answered by BurntSushi
tisonkun asked this question in Q&A
Discussion options

Today I wrota program to catch group capture in a generic way:

let mut result = vec![];
for c in regex.captures_iter(arg) {
 let mut cap = vec![];
 for m in c.iter() {
 if let Some(m) = m {
 // how to get the name?
 cap.push(<{ "name": ..., "text": m.as_str() }>);
 }
 cap.push(<null>);
 }
 result.push(cap);
}

When the match is not named, I'm OK to attach a null name field. But is it possible to get the name of named capture group?

Related - #955 and thus cc @BurntSushi @01mf02

You must be logged in to vote

In the future, please provide an MRE.

It looks like you should be able to just do for (name, m) in regex.capture_names().zip(c.iter()) { via Regex::capture_names.

Replies: 1 comment 1 reply

Comment options

In the future, please provide an MRE.

It looks like you should be able to just do for (name, m) in regex.capture_names().zip(c.iter()) { via Regex::capture_names.

You must be logged in to vote
1 reply
Comment options

Thank you!

Answer selected by BurntSushi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
Converted from issue

This discussion was converted from issue #1267 on May 19, 2025 15:40.

AltStyle によって変換されたページ (->オリジナル) /