Crate regex_syntax [] [src]

This crate provides a regular expression parser and an abstract syntax for regular expressions. The abstract syntax is defined by the Expr type. The concrete syntax is enumerated in the regex crate documentation.

Note that since this crate is first and foremost an implementation detail for the regex crate, it may experience more frequent breaking changes. It is exposed as a separate crate so that others may use it to do analysis on regular expressions or even build their own matching engine.

Example: parsing an expression

Parsing a regular expression can be done with the Expr::parse function.

use regex_syntax::Expr;

assert_eq!(Expr::parse(r"ab|yz").unwrap(), Expr::Alternate(vec![
    Expr::Literal { chars: vec!['a', 'b'], casei: false },
    Expr::Literal { chars: vec!['y', 'z'], casei: false },
]));

Example: inspecting an error

The parser in this crate provides very detailed error values. For example, if an invalid character class range is given:

use regex_syntax::{Expr, ErrorKind};

let err = Expr::parse(r"[z-a]").unwrap_err();
assert_eq!(err.position(), 4);
assert_eq!(err.kind(), &ErrorKind::InvalidClassRange {
    start: 'z',
    end: 'a',
});

Or unbalanced parentheses:

use regex_syntax::{Expr, ErrorKind};

let err = Expr::parse(r"ab(cd").unwrap_err();
assert_eq!(err.position(), 2);
assert_eq!(err.kind(), &ErrorKind::UnclosedParen);

Structs

CharClass

A character class.

ClassRange

A single inclusive range in a character class.

Error

A parse error.

ExprBuilder

A builder for configuring regular expression parsing.

Enums

ErrorKind

The specific type of parse error that can occur.

Expr

A regular expression abstract syntax tree.

Repeater

The type of a repeat operator expression.

Functions

is_punct

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

quote

Escapes all regular expression meta characters in text.

Type Definitions

Result

An alias for computations that can return a Error.