This crate provides routines for searching strings for matches of a regular
expression (aka "regex"). The regex syntax supported by this crate is similar
to other regex engines, but it lacks several features that are not known how to
implement efficiently. This includes, but is not limited to, look-around and
backreferences. In exchange, all regex searches in this crate have worst case
O(m * n) time complexity, where
m is proportional to the size of the regex
n is proportional to the size of the string being searched.
Module documentation with examples. The module documentation also includes a comprehensive description of the syntax supported.
Documentation with examples for the various matching functions and iterators
can be found on the
To bring this crate into your repository, either add
regex to your
Cargo.toml, or run
cargo add regex.
Here's a simple example that matches a date in YYYY-MM-DD format and prints the year, month and day:
If you have lots of dates in text that you'd like to iterate over, then it's easy to adapt the above example with an iterator:
Usage: Avoid compiling the same regex in a loop
It is an anti-pattern to compile the same regular expression in a loop since compilation is typically expensive. (It takes anywhere from a few microseconds to a few milliseconds depending on the size of the regex.) Not only is compilation itself expensive, but this also prevents optimizations that reuse allocations internally to the matching engines.
In Rust, it can sometimes be a pain to pass regular expressions around if
they're used from inside a helper function. Instead, we recommend using the
once_cell crate to ensure that
regular expressions are compiled exactly once. For example:
Specifically, in this example, the regex will be compiled when it is used for the first time. On subsequent uses, it will reuse the previous compilation.
Usage: match regular expressions on
The main API of this crate (
regex::Regex) requires the caller to pass a
&str for searching. In Rust, an
&str is required to be valid UTF-8, which
means the main API can't be used for searching arbitrary bytes.
To match on arbitrary bytes, use the
regex::bytes::Regex API. The API is
identical to the main API, except that it takes an
&[u8] to search on instead
&[u8] APIs also permit disabling Unicode mode in the regex
even when the pattern would match invalid UTF-8. For example,
not allowed in
regex::Regex but is allowed in
(?-u:.) matches any byte except for
. will match the
UTF-8 encoding of any Unicode scalar value except for
This example shows how to find all null-terminated strings in a slice of bytes:
use Regex; let re = new.unwrap; let text = b"foo\xFFbar\x00baz\x00"; // Extract all of the strings without the null terminator from each match. // The unwrap is OK here since a match requires the `cstr` capture to match. let cstrs: = re.captures_iter .map .collect; assert_eq!;
Notice here that the
[^\x00]+ will match any byte except for
including bytes like
\xFF which are not valid UTF-8. When using the main API,
[^\x00]+ would instead match any valid UTF-8 sequence except for
Usage: match multiple regular expressions simultaneously
This demonstrates how to use a
RegexSet to match multiple (possibly
overlapping) regular expressions in a single scan of the search text:
use RegexSet; let set = new.unwrap; // Iterate over and collect all of the matches. let matches: = set.matches.into_iter.collect; assert_eq!; // You can also test whether a particular regex matched: let matches = set.matches; assert!; assert!;
Usage: regex internals as a library
regex-automata directory contains a crate that
exposes all of the internal matching engines used by the
regex crate. The
idea is that the
regex crate exposes a simple API for 99% of use cases, but
regex-automata exposes oodles of customizable behaviors.
Usage: a regular expression parser
This repository contains a crate that provides a well tested regular expression parser, abstract syntax and a high-level intermediate representation for convenient analysis. It provides no facilities for compilation or execution. This may be useful if you're implementing your own regex engine or otherwise need to do analysis on the syntax of a regular expression. It is otherwise not recommended for general use.
This crate comes with several features that permit tweaking the trade off between binary size, compilation time and runtime performance. Users of this crate can selectively disable Unicode tables, or choose from a variety of optimizations performed by this crate to disable.
When all of these features are disabled, runtime match performance may be much
worse, but if you're matching on short strings, or if high performance isn't
necessary, then such a configuration is perfectly serviceable. To disable
all such features, use the following
Cargo.toml dependency configuration:
 = "1.3" = false # Unless you have a specific reason not to, it's good sense to enable standard # library support. It enables several optimizations and avoids spin locks. It # also shouldn't meaningfully impact compile times or binary size. = ["std"]
This will reduce the dependency tree of
regex down to two crates:
The full set of features one can disable are in the "Crate features" section of the documentation.
Minimum Rust version policy
This crate's minimum supported
rustc version is
The policy is that the minimum Rust version required to use this crate can be
increased in minor version updates. For example, if regex 1.0 requires Rust
1.20.0, then regex 1.0.z for all values of
z will also require Rust 1.20.0 or
newer. However, regex 1.y for
y > 0 may require a newer minimum version of
This project is licensed under either of
- Apache License, Version 2.0, (LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or https://opensource.org/licenses/MIT)
at your option.
The data in
regex-syntax/src/unicode_tables/ is licensed under the Unicode