regex library for Rust: complete guide
\n\n1. What it is and why you need it
\n\nThe regex library is the standard and most popular tool for working with regular expressions in the Rust language. It provides a powerful, safe, and high-performance engine for searching, extracting, and replacing text based on patterns. Unlike many other implementations, regex in Rust is designed with an emphasis on memory safety and the absence of undefined behavior, which is critical for systems programming.
Why is it needed? Regular expressions are a universal language for describing text patterns. They can be used to solve a huge range of tasks: validating email addresses and URLs, parsing logs, extracting data from HTML/JSON, searching and replacing in code, parsing user input, and much more. The regex library does this quickly and reliably. It uses an automaton-based algorithm (Thompson NFA) that guarantees linear execution time relative to the length of the input string, protecting against "catastrophic backtracking" inherent in some other engines (e.g., PCRE or Python's re).
Key features of the library: full support for regular expression syntax (anchors, quantifiers, groups, lookahead/lookbehind, Unicode), working with arbitrary byte sequences, the ability to compile expressions once and reuse them, as well as iterator support for finding all matches. The library is part of the Rust ecosystem and is actively maintained by the community.
\n\n2. Installation
\n\nTo install the regex library, the cargo package manager, which is part of Rust, is used. You need to add a dependency to the Cargo.toml file of your project.
Exact command to add via terminal:
\ncargo add regex\n\nOr manually add to the [dependencies] section of the Cargo.toml file:
[dependencies]\nregex = "1"\n\nAfter that, run cargo build to download and compile the library. Version "1" means that the latest stable version from the 1.x series will be used.
3. Quick start — minimal working example
\n\nLet's write a simple program that checks whether a string contains the word "hello" (case-insensitive).
\n\nuse regex::Regex;\n\nfn main() {\n // Compile the regular expression\n // (?i) — flag that disables case sensitivity\n let re = Regex::new(r"(?i)hello").unwrap();\n\n // String to search\n let text = "Hello, world!";\n\n // Check if there is a match\n if re.is_match(text) {\n println!("Match found!");\n } else {\n println!("No matches.");\n }\n}\n\nThis code will output: Match found!. Note the .unwrap() — it is used here for simplicity, but in real code you should handle regular expression compilation errors (e.g., if the pattern is invalid).
4. Main methods
\n\nBelow are the key methods and functions of the regex library. All examples assume you have the import use regex::Regex;.
4.1. Regex::new(pattern: &str) -> Result<Regex, Error>
\nWhat it does: Compiles the string pattern into a regular expression. Returns a Result that needs to be handled in case of a syntax error.
let re = Regex::new(r"^\\d{3}-\\d{2}-\\d{4}$").expect("Invalid regex");\n// re is now ready to use\n\n