[][src]Crate regex_syntax

This crate provides a robust regular expression parser.

This crate defines two primary types:

These two types come with conversion routines:

As a convenience, the above two conversion routines are combined into one via the top-level Parser type. This Parser will first convert your pattern to an Ast and then convert the Ast to an Hir.


This example shows how to parse a pattern string into its HIR:

use regex_syntax::Parser;
use regex_syntax::hir::{self, Hir};

let hir = Parser::new().parse("a|b").unwrap();
assert_eq!(hir, Hir::alternation(vec![

Concrete syntax supported

The concrete syntax is documented as part of the public API of the regex crate.

Input safety

A key feature of this library is that it is safe to use with end user facing input. This plays a significant role in the internal implementation. In particular:

  1. Parsers provide a nest_limit option that permits callers to control how deeply nested a regular expression is allowed to be. This makes it possible to do case analysis over an Ast or an Hir using recursion without worrying about stack overflow.
  2. Since relying on a particular stack size is brittle, this crate goes to great lengths to ensure that all interactions with both the Ast and the Hir do not use recursion. Namely, they use constant stack space and heap space proportional to the size of the original pattern string (in bytes). This includes the type's corresponding destructors. (One exception to this is literal extraction, but this will eventually get fixed.)

Error reporting

The Display implementations on all Error types exposed in this library provide nice human readable errors that are suitable for showing to end users in a monospace font.

Literal extraction

This crate provides limited support for literal extraction from Hir values. Be warned that literal extraction currently uses recursion, and therefore, stack size proportional to the size of the Hir.

The purpose of literal extraction is to speed up searches. That is, if you know a regular expression must match a prefix or suffix literal, then it is often quicker to search for instances of that literal, and then confirm or deny the match using the full regular expression engine. These optimizations are done automatically in the regex crate.



Defines an abstract syntax for regular expressions.


Defines a high-level intermediate representation for regular expressions.



A convenience parser for regular expressions.


A builder for a regular expression parser.



This error type encompasses any error that can be returned by this crate.



Escapes all regular expression meta characters in text.


Escapes all meta characters in text and writes the result into buf.


Returns true if the give character has significance in a regex.


Returns true if and only if the given character is an ASCII word character.


Returns true if and only if the given character is a Unicode word character.

Type Definitions


A type alias for dealing with errors returned by this crate.