XionKB

 

Super Light Regular Expressions

Super Light Regular Expressions, or SLRE, are a subset of Perl Compatible Regular Expressions, provided in the form of an ANSI C library originally authored by Sergey Lyubka and maintained by Aquefir Consulting LLC.

Super Light Regular Expressions
A PCRE2 subset parser library.
Developer Sergey Lyubka, Alexander Nicholi
Maintainer Cesanta Limited (formerly), Aquefir Consulting LLC (current)
Release July 29th, 2013; 13 years ago
Stable release August 14th, 2026; this year
Written in C
Licence GPLv2 only
Repository github.com/aquefir/slre

Introduction

SLRE is an ANSI C library that implements a subset of Perl’s regular expression syntax. The main features of SLRE are:

SLRE is perfect for tasks like parsing network requests, configuration files, user input, etc, when libraries like PCRE are too heavyweight for the given task. Developers of embedded systems would benefit most.

Supported syntax

Expr Description
^ Match beginning of a buffer
$ Match end of a buffer
() Grouping and substring capturing
\s Match whitespace
\S Match non-whitespace
\d Match decimal digit
\n Match line feed character
\r Match carriage return character
\f Match form feed character
\v Match vertical tab character
\t Match horizontal tab character
\b Match backspace character
+ Match one or more times (greedy)
+? Match one or more times (non-greedy)
* Match zero or more times (greedy)
*? Match zero or more times (non-greedy)
? Match zero or once (non-greedy)
x|y Match x or y (alternation operator)
\meta Match one of the meta characters: ^$().[]*+?|\
\xHH Match byte with hex value 0xHH, e.g. \x4a
[...] Match any character from set. Ranges like [a-z] work
[^...] Match any character but ones from set

Unicode support is still under development.

API

slre.h
C
int slre_match( const char * regexp,
const char * buf,
int buf_len,
struct slre_cap * caps,
int num_caps,
int flags );

slre_match() matches string buffer buf of length buf_len against regular expression regexp, which should conform the syntax outlined above. If regexp contains brackets, slre_match() can capture the respective substrings into the array of struct slre_cap structures:

/* Stores matched fragment for the expression inside brackets */ struct slre_cap
{
const char * ptr; /* Points to the matched fragment */
int len;          /* Length of the matched fragment */
};

N-th member of the caps array will contain fragment that corresponds to the N-th opening bracket in the regex; N is zero-based. slre_match() returns number of bytes scanned from the beginning of the string. If return value is greater or equal to 0, there is a match. If return value is less then 0, there is no match. Negative return codes are as follows:

#define SLRE_NO_MATCH              (-1)
#define SLRE_UNEXPECTED_QUANTIFIER (-2)
#define SLRE_UNBALANCED_BRACKETS   (-3)
#define SLRE_INTERNAL_ERROR        (-4)
#define SLRE_INVALID_CHARACTER_SET (-5)
#define SLRE_INVALID_METACHARACTER (-6)
#define SLRE_CAPS_ARRAY_TOO_SMALL  (-7)
#define SLRE_TOO_MANY_BRANCHES     (-8)
#define SLRE_TOO_MANY_BRACKETS     (-9)

Valid flags are: