Introduction ============ TRE is a lightweight, robust, and efficient POSIX compliant regexp matching library with some exciting features such as approximate (fuzzy) matching. The matching algorithm used in TRE uses linear worst-case time in the length of the text being searched, and quadratic worst-case time in the length of the used regular expression. In other words, the time complexity of the algorithm is O(M^2N), where M is the length of the regular expression and N is the length of the text. The used space is also quadratic on the length of the regex, but does not depend on the searched string. This quadratic behaviour occurs only on pathological cases which are probably very rare in practice. Hacking ======= Here's how to work with this code. Prerequisites ------------- You will need the following tools installed on your system: - autoconf - automake - gettext - libtool - zip (optional) Building -------- First, prepare the tre. Change to the root of the source directory and run ``` ./utils/autogen.sh ``` This will regenerate various things using the prerequisite tools so that you end up with a buildable tree. After this, you can run the configure script and build TRE as usual: ``` ./configure make make check make install ``` Building a source code package ------------------------------ In a prepared tree, this command creates a source code tarball: ``` ./configure && make dist ``` Alternatively, you can run ``` ./utils/build-sources.sh ``` which builds the source code packages and puts them in the `dist` subdirectory. This script needs a working `zip` command. Features ======== TRE is not just yet another regexp matcher. TRE has some features which are not there in most free POSIX compatible implementations. Most of these features are not present in non-free implementations either, for that matter. Approximate matching -------------------- Approximate pattern matching allows matches to be approximate, that is, allows the matches to be close to the searched pattern under some measure of closeness. TRE uses the edit-distance measure (also known as the Levenshtein distance) where characters can be inserted, deleted, or substituted in the searched text in order to get an exact match. Each insertion, deletion, or substitution adds the distance, or cost, of the match. TRE can report the matches which have a cost lower than some given threshold value. TRE can also be used to search for matches with the lowest cost. TRE includes a version of the agrep (approximate grep) command line tool for approximate regexp matching in the style of grep. Unlike other agrep implementations (like the one by Sun Wu and Udi Manber from University of Arizona) TRE agrep allows full regexps of any length, any number of errors, and non-uniform costs for insertion, deletion and substitution. Strict standard conformance --------------------------- POSIX defines the behaviour of regexp functions precisely. TRE attempts to conform to these specifications as strictly as possible. TRE always returns the correct matches for subpatterns, for example. Very few other implementations do this correctly. In fact, the only other implementations besides TRE that I am aware of (free or not) that get it right are Rx by Tom Lord, Regex++ by John Maddock, and the AT&T ast regex by Glenn Fowler and Doug McIlroy. The standard TRE tries to conform to is the IEEE Std 1003.1-2001, or Open Group Base Specifications Issue 6, commonly referred to as "POSIX". It can be found online here. The relevant parts are the base specifications on regular expressions (and the rationale) and the description of the regcomp() API. For an excellent survey on POSIX regexp matchers, see the testregex pages by Glenn Fowler of AT&T Labs Research. Predictable matching speed -------------------------- Because of the matching algorithm used in TRE, the maximum time consumed by any regexec() call is always directly proportional to the length of the searched string. There is one exception: if back references are used, the matching may take time that grows exponentially with the length of the string. This is because matching back references is an NP complete problem, and almost certainly requires exponential time to match in the worst case. Predictable and modest memory consumption ----------------------------------------- A regexec() call never allocates memory from the heap. TRE allocates all the memory it needs during a regcomp() call, and some temporary working space from the stack frame for the duration of the regexec() call. The amount of temporary space needed is constant during matching and does not depend on the searched string. For regexps of reasonable size TRE needs less than 50K of dynamically allocated memory during the regcomp() call, less than 20K for the compiled pattern buffer, and less than two kilobytes of temporary working space from the stack frame during a regexec() call. There is no time/memory tradeoff. TRE is also small in code size; statically linking with TRE increases the executable size less than 30K (gcc-3.2, x86, GNU/Linux). Wide character and multibyte character set support -------------------------------------------------- TRE supports multibyte character sets. This makes it possible to use regexps seamlessly with, for example, Japanese locales. TRE also provides a wide character API. Binary pattern and data support ------------------------------- TRE provides APIs which allow binary zero characters both in regexps and searched strings. The standard API cannot be easily used to, for example, search for printable words from binary data (although it is possible with some hacking). Searching for patterns which contain binary zeroes embedded is not possible at all with the standard API. Completely thread safe ---------------------- TRE is completely thread safe. All the exported functions are re-entrant, and a single compiled regexp object can be used simultaneously in multiple contexts; e.g. in main() and a signal handler, or in many threads of a multithreaded application. Portable -------- TRE is portable across multiple platforms. Here's a table of platforms and compilers that have been successfully used to compile and run TRE:
Platform(s) | Compiler(s) |
---|---|
AIX 4.3.2 - 5.3.0 | GCC, C for AIX compiler version 5 |
Compaq Tru64 UNIX V5.1A/B | Compaq C V6.4-014 - V6.5-011 |
Cygwin 1.3 - 1.5 | GCC |
Digital UNIX V4.0 | DEC C V5.9-005 |
FreeBSD 4 and above | GCC |
GNU/Linux systems on x86, x86_64, ppc64, s390 | GCC |
HP-UX 10.20- 11.00 | GCC, HP C Compiler |
IRIX 6.5 | GCC, MIPSpro Compilers 7.3.1.3m |
Max OS X | |
NetBSD 1.5 and above | GCC, egcs |
OpenBSD 3.3 and above | GCC |
Solaris 2.7-10 sparc/x86 | GCC, Sun Workshop 6 compilers |
Windows 98 - XP | Microsoft Visual C++ 6.0 |