Sample code for 30+ languages & platforms
Zig Requires Chilkat v11.7.0+

Search a PDF with a Regular Expression

See more PDF Text Extraction Examples

Demonstrates searching a PDF with a regular expression (regex: true), limiting the number of results (maxResults), and matching whole words only (wholeWord).

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();

    // This example requires the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    // The PDF used in this example is available at order_records.pdf

    const pdf = try chilkat.Pdf.init();
    defer pdf.deinit();
    pdf.loadFile("qa_data/pdf/order_records.pdf") catch {
        std.debug.print("{s}\n", .{try pdf.getLastErrorText(alloc)});
        return;
    };

    const json = try chilkat.JsonObject.init();
    defer json.deinit();
    const json_out = try chilkat.JsonObject.init();
    defer json_out.deinit();

    // Find every order number.  With regex set to true, the query is a regular expression.
    json.updateString("query", "ORD-[0-9]{4}-[0-9]{4}") catch {};
    json.updateBool("regex", true) catch {};
    // We only want the matched text, so no context is needed.
    json.updateInt("context", 0) catch {};

    pdf.extractText("search", json, json_out) catch {
        std.debug.print("{s}\n", .{try pdf.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("Order numbers found: {d}\n", .{json_out.intOf("count")});
    var i: i32 = 0;
    var count_i: i32 = json_out.sizeOfArray("matches");
    while (i < count_i) {
        json_out.setI(i);
        std.debug.print("{s}\n", .{try json_out.stringOf(alloc, "matches[i].text")});
        i = i + 1;
    }

    // Find the e-mail addresses, but stop after the first three.
    json.updateString("query", "[a-z0-9._]+@[a-z]+[.][a-z]+") catch {};
    json.updateInt("maxResults", 3) catch {};
    pdf.extractText("search", json, json_out) catch {
        std.debug.print("{s}\n", .{try pdf.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("First three e-mail addresses:\n", .{});
    i = 0;
    count_i = json_out.sizeOfArray("matches");
    while (i < count_i) {
        json_out.setI(i);
        std.debug.print("{s}\n", .{try json_out.stringOf(alloc, "matches[i].text")});
        i = i + 1;
    }

    // wholeWord: true matches only whole words.  The word "cat" appears once in the PDF,
    // but it is also inside "catalog" and "category".
    json.delete("maxResults") catch {};
    json.updateBool("regex", false) catch {};
    json.updateString("query", "cat") catch {};
    pdf.extractText("search", json, json_out) catch {
        std.debug.print("{s}\n", .{try pdf.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("Occurrences of cat (any): {d}\n", .{json_out.intOf("count")});

    json.updateBool("wholeWord", true) catch {};
    pdf.extractText("search", json, json_out) catch {
        std.debug.print("{s}\n", .{try pdf.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("Occurrences of cat (whole word): {d}\n", .{json_out.intOf("count")});

    // Sample output:

    // Order numbers found: 9
    // ORD-2026-0301
    // ORD-2026-0302
    // ORD-2026-0303
    // ORD-2026-0304
    // ORD-2026-0305
    // ORD-2026-0306
    // ORD-2026-0307
    // ORD-2026-0308
    // ORD-2026-0307
    // First three e-mail addresses:
    // alice.moore@example.com
    // bkumar@example.org
    // carla@example.net
    // Occurrences of cat (any): 3
    // Occurrences of cat (whole word): 1
}