kokke/tiny-regex-c logo

kokke/tiny-regex-c

Free

Small portable regex in C

FreeFree tier
Type
Open Source

About kokke/tiny-regex-c

A small, portable, and self-contained regular expression library written in C. Inspired by Rob Pike's regex code from the book 'Beautiful Code', it implements a subset of Python's re-module syntax. Designed for resource-constrained environments, it compiles to a ~3KB binary, uses no dynamic memory allocation, and prefers iterative searching over recursive to avoid stack exhaustion. The library is thoroughly tested using exrex for random test case generation and includes a verification harness for the KLEE symbolic execution engine.

Key Features

Small code & binary size: ~500 SLOC, ~3KB on x86
Statically defined memory usage, no dynamic allocation (no malloc/free)
Iterative searching preferred over recursive to avoid call-stack exhaustion
Supports a subset of Python's re-module syntax
Provides match index and character length of matches
Thorough testing with exrex random test case generation
Includes verification harness for KLEE symbolic execution engine
Compiles to ~2-3KB code space and ~0.5KB RAM on x86

Pros & Cons

Pros
  • Extremely small footprint (code and memory)
  • Portable C code with no external dependencies
  • No dynamic memory allocation, safe for constrained systems
  • Thoroughly tested with random test cases and symbolic verification
  • Clear, well-documented code adhering to design goals
Cons
  • No support for capturing groups or named capture groups
  • Inverted character classes are buggy (as noted in documentation)
  • Limited feature set compared to full regex libraries (no lookahead, backreferences, etc.)
  • Requires manual integration and compilation into target project

Best For

Embedding regular expression matching in C programsResource-constrained environments (e.g., embedded systems, IoT, firmware)Educational use for studying regex implementationSituations requiring a small, self-contained regex library without dependencies

FAQ

What regex operators are supported?
Supports ., ^, $, *, +, ?, character classes ([abc]), inverted classes ([^abc]), character ranges ([a-zA-Z]), whitespace (\s), non-whitespace (\S), alphanumeric (\w), non-alphanumeric (\W), digits (\d), and non-digits (\D).
Does the library use dynamic memory allocation?
No. Memory usage is statically #define'd, and there are no calls to malloc or free.
Is the library tested?
Yes, it is thoroughly tested using exrex to randomly generate test cases from regex patterns, and includes a verification harness for the KLEE Symbolic Execution Engine.
Can I use capture groups?
No. The library does not support capturing groups or named capture groups as of the current version.