Skip to content

leonardoce/slre

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SLRE - Super Light Regular Expression library

This is a fork of the Super Light Regular Expression Library by Sergey Lyubka at version 1.0, when the library was Beer-ware license.

I've taken the source code from:

http://slre.sourceforge.net/

Original documentation

SLRE is an ANSI C library that implements a tiny subset of Perl regular expressions. It is primarily targeted for developers who want to parse configuation files, where speed is unimportant. It is in single .c file, easily modifiable for custom needs. For example, if one wants to introduce a new metacharacter, '\i', that means 'IP address', it is easy to do so. Features:

  • Crossplatform - pure ANSI C
  • Very simple API
  • Light: about 5kB of code when compiled
  • Uses no dynamic memory allocation
  • Thread safe

Supported RE Syntax

  ^               Match beginning of a buffer
  $               Match end of a buffer
  ()              Grouping and substring capturing
  [...]           Match any character from set
  [^...]          Match any character but ones from set
  \s              Match whitespace
  \S              Match non-whitespace
  \d              Match decimal digit
  \r              Match carriage return
  \n              Match newline
  +               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
  \xDD            Match byte with hex value 0xDD
  \meta           Match one of the meta character: ^$().[*+?\

API

Two functions represent the API: one is used for compilations of the RE, the other for performing a match. Both functions return 0 on error, and 1 on success. If the round brackets are used in the RE, then matched substrings can be returned back to the caller in the 'struct cap *' array. Array size must be enough to hold all matches: array_size = number_of_round_bracket_pairs + 1. The first element of the array will always hold the substring matched by the whole RE.

/* Captured substring */
struct cap {
        const char      *ptr;           /* Pointer to the substring     */
        int             len;            /* Substring length             */
};

int slre_compile(struct slre *, const char *re);
int slre_match(const struct slre *, const char *buf, int buf_len,
        struct cap *captured_substrings);

Usage example

This example shows how to parse HTTP request line:

struct slre        slre;
struct cap         captures[4 + 1];

if (!slre_compile(&slre, "^(GET|POST) (\S+) HTTP/(\S+?)\r\n") {
	printf("Error compiling RE: %s\n", slre.err_str);
} else if (!slre_match(&slre, buf, len, captures)) {
	printf("Not a valid HTTP request\n" );
} else {
	printf("Request line length: %d\n", captures[0].len);
	printf("Method: %.*s\n", captures[1].len, captures[1].ptr);
	printf("URI: %.*s\n", captures[2].len, captures[2].ptr);
}

About

Super Light Regular Expression Library (Beerware licensed one)

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages