thng292/zregex
Zig bindings for POSIX's regex
This library wraps the C regex library and provides a convenient API.
Compatible with zig version 0.13.0
Note: This library used the C's allocator to allocate the memory for regex_t
struct. More info at adapter.c
zig fetch --save git+https://github.com/thng292/zregex.git
build.zig
const zregex = b.dependency("zregex", .{
.target = target,
.optimize = optimize,
});
exe.root_module.addImport("zregex", zregex.module("zregex"));
const Regex = @import("zregex");
const regex = try Regex.init("[0-9]\\{1\\}", .{});
defer regex.deinit();
try std.testing.expect(regex.match("12312", .{}));
try std.testing.expect(!regex.match("abc", .{}));
const regex = try Regex.init("[ab]c", .{});
defer regex.deinit();
var iter = regex.exec("bc cc", .{});
if (iter.next()) |matched| {
try std.testing.expectEqualStrings("bc", matched);
}
if (iter.next()) |matched| {
try std.testing.expectEqualStrings("cc", matched);
}
if (iter.next()) |_| {
unreachable;
}
const regex = try Regex.init("\\([ab]c\\)", .{});
defer regex.deinit();
try std.testing.expectEqual(1, regex.getNumSubexpression());
fn cerrToZig(err_num: c_int) Error {
return switch (err_num) {
c.REG_NOMATCH => error.NoMatch,
c.REG_BADPAT => error.InvalidRegex,
c.REG_ECOLLATE => error.InvalidCollate,
c.REG_ECTYPE => error.InvalidClassType,
c.REG_EESCAPE => error.TrailingBackSlash,
c.REG_ESUBREG => error.InvalidNumberInDigit,
c.REG_EBRACK => error.BracketImbalance,
c.REG_EPAREN => error.ParenthesisImbalance,
c.REG_EBRACE => error.BraceImbalance,
c.REG_BADBR => error.InvalidContentInBrace,
c.REG_ERANGE => error.InvalidEndpoint,
c.REG_ESPACE => error.OutOfMemory,
c.REG_BADRPT => error.InvalidRepeatQuantifier,
c.REG_ENOSYS => error.NotImplemented,
else => error.Unknown,
};
}