API Reference

regex

Fast regular expression engine for Rust with bounded, linear-time matching and Unicode support.

Official Documentation ↗
CompatibilityThis documentation covers version v1. Check the official docs for the latest API surface.

Installation

cargocargo add regex

Methods

Fast regular expression engine for Rust with bounded, linear-time matching and Unicode support.

Overview

The regex crate is Rust’s standard matcher: compile a pattern once, then is_match, find, captures, and replace run in linear time over the haystack. Unicode classes are on by default, so \w and case handling behave correctly for non-ASCII text.

Captures support named groups, replacements take group references or closures, and find_iter walks matches with byte offsets. The deliberate absence of lookarounds and backreferences is what keeps worst-case matching fast.

Core Methods

The most frequently used entry points, with signatures and runnable examples:

  • Regex::new — Compiles a pattern once; reuse the value because compilation is expensive.
  • captures / find — Extracts capture groups by index or name, returning matched spans.
  • replace_all — Replaces every match using $1-style group references or a closure computing the text.
  • find_iter — Iterates non-overlapping matches as Match values with start and end offsets.

Usage Notes

  • Store compiled patterns in statics with LazyLock or once_cell to avoid recompiles.
  • Prefer named groups over numeric indexes for readable extraction code.
  • Validate input length before matching to bound work on hostile payloads.

Common Pitfalls

  • Compiling inside a loop burns CPU, compilation is far costlier than matching.
  • Caps indexing panics on non-participating optional groups; use caps.name or get.
  • \d in the default Unicode mode matches non-ASCII digits; restrict with [0-9] when the format demands it.

Errors

The following points are quoted verbatim from the official documentation. 来源:https://docs.rs/regex · 取证方式:HTTP GET 官方 README/文档页(HTML 去标签) · 抓取时间:2026-11-07T13:52:06.937Z. Any error type, throw condition or recovery step not stated there is left unspecified; this page does not infer exception classes or return values.

  • (A top-level Regex also permits disabling Unicode and thus matching as if it were one byte at a time, but only when doing so wouldn’t permit matching invalid UTF-8.) When Unicode mode is enabled (the default), . (https://docs.rs/regex)
  • However, this behavior can be disabled by turning off the u flag, even if doing so could result in matching invalid UTF-8. (https://docs.rs/regex)
  • For example, (?-u:\w) is an ASCII-only \w character class and is legal in an &str -based Regex , but (?-u:\W) will attempt to match any byte that isn’t in (?-u:\w) , which in turn includes bytes that are invalid UTF-8. (https://docs.rs/regex)

Security notes

Quoted verbatim from the official documentation; this is not a complete threat model. 来源:https://docs.rs/regex · 取证方式:HTTP GET 官方 README/文档页(HTML 去标签) · 抓取时间:2026-11-07T13:52:06.937Z.

  • Untrusted input discusses how this crate deals with regex patterns or haystacks that are untrusted. (https://docs.rs/regex)
  • We use Rust’s raw strings to avoid needing to deal with escape sequences in both the regex pattern syntax and in Rust’s string literal syntax. (https://docs.rs/regex)
  • For escaping a single space character, you can escape it directly with \ , use its hex character code \x20 or temporarily disable the x flag, e.g., (?-x: ) . (https://docs.rs/regex)

FAQ

Why no lookaround or backreferences?
The engine guarantees linear-time matching, which is incompatible with lookarounds and backreferences, those features can blow up exponentially. Reformulate with capture groups and post-processing, or reach for the regex crate’s lighter sibling fanci patterns only when truly required.
How do I reuse compiled regexes efficiently?
Compile once and share. In async or threaded code, use once_cell::sync::Lazy or std LazyLock for a static Regex, so every call site reuses the same compiled object.
How does . match newlines?
By default it does not. Enable the dot_matches_new_line flag, (?s) inline, when patterns must span lines, and remember ^ and $ match line boundaries only with the multi-line flag, (?m).