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

alexnask/ctregex.zig

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

17 Commits

Repository files navigation

Zig compile time regular expressions

Generating fast code since 2020

Features

  • Comptime regular expression compilation
  • Comptime and runtime matching
  • UTF8, UTF16le, ASCII, codepoint array support
  • Captures (with named (:<name>...) support)
  • |, *, +, ?, (:?...), [...], [^...], {N}, {min,}, {min,max}
  • '\d', '\s' character classes

TODO

  • Faster generated code using DFAs when possible
  • search, findAll, etc.
  • More character classes
  • More features (backreferences etc.)

Example

test "runtime matching" {
 @setEvalBranchQuota(1250);
 // The encoding is utf8 by default, you can use .ascii, .utf16le, .codepoint here instead.
 if (try match("(?<test>def|abc)([πŸ˜‡Ο‰])+", .{.encoding = .utf8}, "abcπŸ˜‡Ο‰Ο‰Ο‰Ο‰Ο‰")) |res| {
 std.debug.warn("Test: {}, 1: {}\n", .{ res.capture("test"), res.captures[1] });
 }
}
test "comptime matching" {
 @setEvalBranchQuota(2700);
 if (comptime try match("(?<test>def|abc)([πŸ˜‡Ο‰])+", .{}, "abcπŸ˜‡Ο‰Ο‰Ο‰Ο‰Ο‰")) |res| {
 @compileError("Test: " ++ res.capture("test").? ++ ", 1: " ++ res.captures[1].?);
 }
}

See tests.zig for more examples.
Small benchmark with ctregex, PCRE2

AltStyle γ«γ‚ˆγ£γ¦ε€‰ζ›γ•γ‚ŒγŸγƒšγƒΌγ‚Έ (->γ‚ͺγƒͺγ‚ΈγƒŠγƒ«) /