Project Name | Stars | Downloads | Repos Using This | Packages Using This | Most Recent Commit | Total Releases | Latest Release | Open Issues | License | Language |
---|---|---|---|---|---|---|---|---|---|---|
Path To Regexp | 7,619 | 534,174 | 5,882 | 2 months ago | 53 | May 06, 2022 | 20 | mit | TypeScript | |
Turn a path string such as `/user/:name` into a regular expression | ||||||||||
Super Expressive | 4,474 | 4 months ago | 10 | mit | JavaScript | |||||
🦜 Super Expressive is a zero-dependency JavaScript library for building regular expressions in (almost) natural language | ||||||||||
Randexp.js | 1,769 | 19,706 | 389 | 6 months ago | 20 | July 21, 2018 | 11 | mit | JavaScript | |
Create random strings that match a given regular expression. | ||||||||||
Frak | 1,056 | 4 years ago | 9 | February 09, 2020 | 1 | Clojure | ||||
Transform collections of strings into regular expressions. | ||||||||||
Tiny Regex C | 1,005 | 9 months ago | 32 | unlicense | C | |||||
Small portable regex in C | ||||||||||
Regexp Examples | 483 | 62 | 15 | 3 years ago | 42 | January 09, 2020 | 8 | mit | Ruby | |
Generate strings that match a given regular expression | ||||||||||
Re Build | 472 | 6 years ago | 2 | July 25, 2016 | mit | JavaScript | ||||
Building regular expressions with natural language | ||||||||||
Escape String Regexp | 436 | 773,946 | 2,432 | 2 years ago | 10 | April 17, 2021 | 1 | mit | JavaScript | |
Escape RegExp special characters | ||||||||||
Teip | 432 | 7 months ago | 6 | April 28, 2022 | 4 | mit | Rust | |||
Masking tape to help commands "do one thing well" | ||||||||||
Node Re2 | 428 | 40 | 55 | a month ago | 52 | June 12, 2022 | 3 | other | JavaScript | |
node.js bindings for RE2: fast, safe alternative to backtracking regular expression engines. |
Small and portable Regular Expression (regex) library written in C.
Design is inspired by Rob Pike's regex-code for the book "Beautiful Code" available online here.
Supports a subset of the syntax and semantics of the Python standard library implementation (the re
-module).
I will gladly accept patches correcting bugs.
The main design goal of this library is to be small, correct, self contained and use few resources while retaining acceptable performance and feature completeness. Clarity of the code is also highly valued.
malloc
/ free
).(^P<name>group)
etc.make test
to generate a few thousand tests cases yourself.> gcc -Os -c re.c
> size re.o
text data bss dec hex filename
2404 0 304 2708 a94 re.o
This is the public / exported API:
/* Typedef'd pointer to hide implementation details. */
typedef struct regex_t* re_t;
/* Compiles regex string pattern to a regex_t-array. */
re_t re_compile(const char* pattern);
/* Finds matches of the compiled pattern inside text. */
int re_matchp(re_t pattern, const char* text, int* matchlength);
/* Finds matches of pattern inside text (compiles first automatically). */
int re_match(const char* pattern, const char* text, int* matchlength);
The following features / regex-operators are supported by this library.
NOTE: inverted character classes are buggy - see the test harness for concrete examples.
.
Dot, matches any character^
Start anchor, matches beginning of string$
End anchor, matches end of string*
Asterisk, match zero or more (greedy)+
Plus, match one or more (greedy)?
Question, match zero or one (non-greedy)[abc]
Character class, match if one of {'a', 'b', 'c'}[^abc]
Inverted class, match if NOT one of {'a', 'b', 'c'}[a-zA-Z]
Character ranges, the character set of the ranges { a-z | A-Z }\s
Whitespace, \t \f \r \n \v and spaces\S
Non-whitespace\w
Alphanumeric, [a-zA-Z0-9_]\W
Non-alphanumeric\d
Digits, [0-9]\D
Non-digitsCompile a regex from ASCII-string (char-array) to a custom pattern structure using re_compile()
.
Search a text-string for a regex and get an index into the string, using re_match()
or re_matchp()
.
The returned index points to the first place in the string, where the regex pattern matches.
The integer pointer passed will hold the length of the match.
If the regular expression doesn't match, the matching function returns an index of -1 to indicate failure.
Example of usage:
/* Standard int to hold length of match */
int match_length;
/* Standard null-terminated C-string to search: */
const char* string_to_search = "ahem.. 'hello world !' ..";
/* Compile a simple regular expression using character classes, meta-char and greedy + non-greedy quantifiers: */
re_t pattern = re_compile("[Hh]ello [Ww]orld\\s*[!]?");
/* Check if the regex matches the text: */
int match_idx = re_matchp(pattern, string_to_search, &match_length);
if (match_idx != -1)
{
printf("match at idx %i, %i chars long.\n", match_idx, match_length);
}
For more usage examples I encourage you to look at the code in the tests
-folder.
|
), and see if that can lead us closer to groups as well, e.g. (a|b)+
.example.c
that demonstrates usage.tests/test_perf.c
for performance and time measurements.Q: What differentiates this library from other C regex implementations?
A: Well, the small size for one. 500 lines of C-code compiling to 2-3kb ROM, using very little RAM.
All material in this repository is in the public domain.