Sample code for 30+ languages & platforms
Rust Requires Chilkat v11.1.0+

Regular Expression with Multiple Matches and Named Capture Groups

See more Regular Expressions Examples

Demonstrates regular expressions with named capture groups and multiple matches.

Chilkat Rust Downloads

Rust

let sb = chilkat::StringBuilder::new();
let crlf = true;
let _ = sb.append_line("Name: John Smith", crlf);
let _ = sb.append_line("Name: Jack Johnson", crlf);
let _ = sb.append_line("Name: Mary Adams", crlf);

println!("{}", sb.get_as_string().unwrap_or_default());

// We have the following string:
// Name: John Smith
// Name: Jack Johnson
// Name: Mary Adams

let pattern = "Name:\\s+(?<first>\\w+)\\s+(?<last>\\w+)".to_string();
let json = chilkat::JsonObject::new();
json.set_emit_compact(false);

let timeout_ms = 2000;
let num_matches = sb.regex_match(&pattern, &json, timeout_ms);
if num_matches < 0 {
    // Probably an error in the regular expression.
    // Suggestion: Use AI to help create and/or diagnose regular expressions.
    println!("{}", sb.last_error_text());
    return;
}

// Examine the matches:
println!("{}", json.emit().unwrap_or_default());

// Here is the JSON showing the matches.
// Important:  Capture group 0 always contains the entire match — that is, the portion of the input string that matches the full regular expression.

// {
//   "named": {
//     "first": 1,
//     "last": 2
//   },
//   "match": [
//     {
//       "group": [
//         {
//           "cap": "Name: John Smith",
//           "idx": 0,
//           "len": 16
//         },
//         {
//           "cap": "John",
//           "idx": 6,
//           "len": 4
//         },
//         {
//           "cap": "Smith",
//           "idx": 11,
//           "len": 5
//         }
//       ]
//     },
//     {
//       "group": [
//         {
//           "cap": "Name: Jack Johnson",
//           "idx": 18,
//           "len": 18
//         },
//         {
//           "cap": "Jack",
//           "idx": 24,
//           "len": 4
//         },
//         {
//           "cap": "Johnson",
//           "idx": 29,
//           "len": 7
//         }
//       ]
//     },
//     {
//       "group": [
//         {
//           "cap": "Name: Mary Adams",
//           "idx": 38,
//           "len": 16
//         },
//         {
//           "cap": "Mary",
//           "idx": 44,
//           "len": 4
//         },
//         {
//           "cap": "Adams",
//           "idx": 49,
//           "len": 5
//         }
//       ]
//     }
//   ]
// }

// The capture group index is obtained by looking up the name in the JSON result.
// For example:

let idx_first = json.int_of("named.first");
let idx_last = json.int_of("named.last");

let mut i = 0;
let match_count = json.size_of_array("match");
while i < match_count {
    println!("Match {}:", i + 1);
    json.set_i(i);

    json.set_j(idx_first);
    println!("first: {}", json.string_of("match[i].group[j].cap").unwrap_or_default());

    json.set_j(idx_last);
    println!("first: {}", json.string_of("match[i].group[j].cap").unwrap_or_default());

    println!("");
    i = i + 1;
}

// Output is: 

// Match 1:
// first: John
// first: Smith
// 
// Match 2:
// first: Jack
// first: Johnson
// 
// Match 3:
// first: Mary
// first: Adams