[][src]Crate regress

regress - REGex in Rust with EcmaScript Syntax

This crate provides a regular expression engine which targets EcmaScript (aka JavaScript) regular expression syntax.

Example: test if a string contains a match

use regress::Regex;
let re = Regex::new(r"\d{4}").unwrap();
let matched = re.find("2020-20-05").is_some();

Example: iterating over matches

Here we use a backreference to find doubled characters:

use regress::Regex;
let re = Regex::new(r"(\w)\1").unwrap();
let text = "Frankly, Miss Piggy, I don't give a hoot!";
for m in re.find_iter(text) {
    println!("{}", &text[m.total()])
// Output: ss
// Output: gg
// Output: oo

Example: using capture groups

Capture groups are available in the Match object produced by a successful match. A capture group is a range of byte indexes into the original string.

use regress::Regex;
let re = Regex::new(r"(\d{4})").unwrap();
let text = "Today is 2020-20-05";
let m = re.find(text).unwrap();
let group = m.group(1).unwrap();
println!("Year: {}", &text[group]);
// Output: Year: 2020

Supported Syntax

regress targets ES 2018 syntax. You can refer to the many resources about JavaScript regex syntax.

There are some features which have yet to be implemented:

  • Named capture groups like (?<count>\d+)
  • Named character classes liks [[:alpha:]]
  • Unicode property escapes like \p{Sc}

Note the parser assumes the u (Unicode) flag, as the non-Unicode path is tied to JS's UCS-2 string encoding and the semantics cannot be usefully expressed in Rust.

Unicode remarks

regress supports Unicode case folding. For example:

use regress::{Regex, Flags};
let re = Regex::newf("\u{00B5}", Flags::from("i")).unwrap();

Here the U+00B5 (micro sign) was case-insensitively matched against U+03BC (small letter mu).

regress does NOT perform normalization. For example, e-with-accute-accent can be precomposed or decomposed, and these are treated as not equivalent:

use regress::{Regex, Flags};
let re = Regex::new("\u{00E9}").unwrap();

This agrees with JavaScript semantics. Perform any required normalization before regex matching.

Ascii matching

regress has an "ASCII mode" which treats each 8-bit quantity as a separate character. This may provide improved performance if you do not need Unicode semantics, because it can avoid decoding UTF-8 and has simpler (ASCII-only) case-folding.


use regress::{Flags, Regex};
let re = Regex::newf("BC", Flags::from("i")).unwrap();

Comparison to regex crate

regress supports features that regex does not, in particular backreferences and zero-width lookaround assertions. However the regex crate provides linear-time matching guarantees, while regress does not. This difference is due to the architecture: regex uses finite automata while regress uses "classical backtracking."


regress has a parser, intermediate representation, optimizer which acts on the IR, bytecode emitter, and two bytecode interpreters, referred to as "backends".

The major interpreter is the "classical backtracking" which uses an explicit backtracking stack, similar to JS implementations. There is also the "PikeVM" pseudo-toy backend which is mainly used for testing and verification.



Represents an error encountered during regex compilation. The text contains a human-readable error message.


Flags used to control regex parsing. The default flags are case-sensitive, not-multiline, and optimizing.


A Match represents a portion of a string which was found to match a Regex.


A Regex is the compiled version of a pattern.

Type Definitions


An iterator type which yields Matches found in a string, supporting ASCII only.


An iterator type which yields Matches found in a string.


Range is used to express the extent of a match, as indexes into the input string.