Sample code for 30+ languages & platforms
Zig 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 Zig Downloads

Zig
const std = @import("std");
const chilkat = @import("chilkat");

pub fn main(init: std.process.Init) !void {
    const alloc = init.arena.allocator();

    const sb = try chilkat.StringBuilder.init();
    defer sb.deinit();
    const crlf = true;
    sb.appendLine("Name: John Smith", crlf) catch {};
    sb.appendLine("Name: Jack Johnson", crlf) catch {};
    sb.appendLine("Name: Mary Adams", crlf) catch {};

    std.debug.print("{s}\n", .{try sb.getAsString(alloc)});

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

    const pattern = "Name:\\s+(?<first>\\w+)\\s+(?<last>\\w+)";
    const json = try chilkat.JsonObject.init();
    defer json.deinit();
    json.setEmitCompact(false);

    const timeout_ms = 2000;
    const num_matches = sb.regexMatch(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.
        std.debug.print("{s}\n", .{try sb.getLastErrorText(alloc)});
        return;
    }

    // Examine the matches:
    std.debug.print("{s}\n", .{try json.emit(alloc)});

    // 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:

    const idx_first = json.intOf("named.first");
    const idx_last = json.intOf("named.last");

    var i: i32 = 0;
    const match_count = json.sizeOfArray("match");
    while (i < match_count) {
        std.debug.print("Match {d}:\n", .{i + 1});
        json.setI(i);

        json.setJ(idx_first);
        std.debug.print("first: {s}\n", .{try json.stringOf(alloc, "match[i].group[j].cap")});

        json.setJ(idx_last);
        std.debug.print("first: {s}\n", .{try json.stringOf(alloc, "match[i].group[j].cap")});

        std.debug.print("\n", .{});
        i = i + 1;
    }

    // Output is:

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