Crate regex_automata[][src]

A low level regular expression library that uses deterministic finite automata. It supports a rich syntax with Unicode support, has extensive options for configuring the best space vs time trade off for your use case and provides support for cheap deserialization of automata for use in no_std environments.

Overview

This section gives a brief overview of the primary types in this crate:

Example: basic regex searching

This example shows how to compile a regex using the default configuration and then use it to find matches in a byte string:

use regex_automata::Regex;

let re = Regex::new(r"[0-9]{4}-[0-9]{2}-[0-9]{2}").unwrap();
let text = b"2018-12-24 2016-10-08";
let matches: Vec<(usize, usize)> = re.find_iter(text).collect();
assert_eq!(matches, vec![(0, 10), (11, 21)]);

Example: use sparse DFAs

By default, compiling a regex will use dense DFAs internally. This uses more memory, but executes searches more quickly. If you can abide slower searches (somewhere around 3-5x), then sparse DFAs might make more sense since they can use significantly less space.

Using sparse DFAs is as easy as using Regex::new_sparse instead of Regex::new:

use regex_automata::Regex;

let re = Regex::new_sparse(r"[0-9]{4}-[0-9]{2}-[0-9]{2}").unwrap();
let text = b"2018-12-24 2016-10-08";
let matches: Vec<(usize, usize)> = re.find_iter(text).collect();
assert_eq!(matches, vec![(0, 10), (11, 21)]);

If you already have dense DFAs for some reason, they can be converted to sparse DFAs and used to build a new Regex. For example:

use regex_automata::Regex;

let dense_re = Regex::new(r"[0-9]{4}-[0-9]{2}-[0-9]{2}").unwrap();
let sparse_re = Regex::from_dfas(
    dense_re.forward().to_sparse()?,
    dense_re.reverse().to_sparse()?,
);
let text = b"2018-12-24 2016-10-08";
let matches: Vec<(usize, usize)> = sparse_re.find_iter(text).collect();
assert_eq!(matches, vec![(0, 10), (11, 21)]);

Example: deserialize a DFA

This shows how to first serialize a DFA into raw bytes, and then deserialize those raw bytes back into a DFA. While this particular example is a bit contrived, this same technique can be used in your program to deserialize a DFA at start up time or by memory mapping a file. In particular, deserialization is guaranteed to be cheap because it will always be a constant time operation.

use regex_automata::{DenseDFA, Regex};

let re1 = Regex::new(r"[0-9]{4}-[0-9]{2}-[0-9]{2}").unwrap();
// serialize both the forward and reverse DFAs, see note below
let fwd_bytes = re1.forward().to_u16()?.to_bytes_native_endian()?;
let rev_bytes = re1.reverse().to_u16()?.to_bytes_native_endian()?;
// now deserialize both---we need to specify the correct type!
let fwd: DenseDFA<&[u16], u16> = unsafe { DenseDFA::from_bytes(&fwd_bytes) };
let rev: DenseDFA<&[u16], u16> = unsafe { DenseDFA::from_bytes(&rev_bytes) };
// finally, reconstruct our regex
let re2 = Regex::from_dfas(fwd, rev);

// we can use it like normal
let text = b"2018-12-24 2016-10-08";
let matches: Vec<(usize, usize)> = re2.find_iter(text).collect();
assert_eq!(matches, vec![(0, 10), (11, 21)]);

There are a few points worth noting here:

The same process can be achieved with sparse DFAs as well:

use regex_automata::{SparseDFA, Regex};

let re1 = Regex::new(r"[0-9]{4}-[0-9]{2}-[0-9]{2}").unwrap();
// serialize both
let fwd_bytes = re1.forward().to_u16()?.to_sparse()?.to_bytes_native_endian()?;
let rev_bytes = re1.reverse().to_u16()?.to_sparse()?.to_bytes_native_endian()?;
// now deserialize both---we need to specify the correct type!
let fwd: SparseDFA<&[u8], u16> = unsafe { SparseDFA::from_bytes(&fwd_bytes) };
let rev: SparseDFA<&[u8], u16> = unsafe { SparseDFA::from_bytes(&rev_bytes) };
// finally, reconstruct our regex
let re2 = Regex::from_dfas(fwd, rev);

// we can use it like normal
let text = b"2018-12-24 2016-10-08";
let matches: Vec<(usize, usize)> = re2.find_iter(text).collect();
assert_eq!(matches, vec![(0, 10), (11, 21)]);

Note that unlike dense DFAs, sparse DFAs have no alignment requirements. Conversely, dense DFAs must be be aligned to the same alignment as their state identifier representation.

Support for no_std

This crate comes with a std feature that is enabled by default. When the std feature is enabled, the API of this crate will include the facilities necessary for compiling, serializing, deserializing and searching with regular expressions. When the std feature is disabled, the API of this crate will shrink such that it only includes the facilities necessary for deserializing and searching with regular expressions.

The intended workflow for no_std environments is thus as follows:

Deserialization can happen anywhere. For example, with bytes embedded into a binary or with a file memory mapped at runtime.

Note that the ucd-generate tool will do the first step for you with its dfa or regex sub-commands.

Syntax

This crate supports the same syntax as the regex crate, since they share the same parser. You can find an exhaustive list of supported syntax in the documentation for the regex crate.

Currently, there are a couple limitations. In general, this crate does not support zero-width assertions, although they may be added in the future. This includes:

It is possible to run a search that is anchored at the beginning of the input. To do that, set the RegexBuilder::anchored option when building a regex. By default, all searches are unanchored.

Differences with the regex crate

The main goal of the regex crate is to serve as a general purpose regular expression engine. It aims to automatically balance low compile times, fast search times and low memory usage, while also providing a convenient API for users. In contrast, this crate provides a lower level regular expression interface that is a bit less convenient while providing more explicit control over memory usage and search times.

Here are some specific negative differences:

With some of the downsides out of the way, here are some positive differences:

Modules

dense

Types and routines specific to dense DFAs.

sparse

Types and routines specific to sparse DFAs.

Structs

Error

An error that occurred during the construction of a DFA.

Regex

A regular expression that uses deterministic finite automata for fast searching.

RegexBuilder

A builder for a regex based on deterministic finite automatons.

Enums

DenseDFA

A dense table-based deterministic finite automaton (DFA).

ErrorKind

The kind of error that occurred.

SparseDFA

A sparse table-based deterministic finite automaton (DFA).

Traits

DFA

A trait describing the interface of a deterministic finite automaton (DFA).

StateID

A trait describing the representation of a DFA’s state identifier.