Module regex
Regular expression engine with support for Unicode.
regex is a high-performance regular expression engine for Rust
that provides safe, Unicode-aware pattern matching.
The primary interface is the Regex type,
which represents a compiled regular expression
that can be used to match patterns in text.
The engine is built on finite automata
and provides linear time matching guarantees.
Key features include:
- Full Unicode support by default
- Linear time matching (no exponential backtracking)
- Rich capture group support with named captures
- Multi-line and case-insensitive matching
- Zero-copy string splitting and replacement
For repeated use it's more efficient to compile
the pattern once with Regex::new and reuse it,
calling methods like Regex::is_match on the compiled pattern.
Examples
Basic pattern matching:
use Regex;
let re = new.unwrap;
let text = "Today's date is 2023-12-25";
assert!;
if let Some = re.find
Using capture groups to extract parts:
use Regex;
let re = new.unwrap;
let text = "Birthday: 1985-06-15";
if let Some = re.captures
Finding all matches in a string:
use Regex;
let re = new.unwrap;
let text = "Contact us at support@example.com or admin@test.org";
for mat in re.find_iter
// Output:
// Email: support@example.com
// Email: admin@test.org
String replacement with capture groups:
use Regex;
let re = new.unwrap;
let text = "Date: 2023-12-25";
let result = re.replace;
assert_eq!;
// Replace all occurrences
let text = "Dates: 2023-12-25 and 2024-01-01";
let result = re.replace_all;
assert_eq!;
Case-insensitive matching:
use RegexBuilder;
let re = new
.case_insensitive
.build
.unwrap;
assert!;
assert!;
assert!;
Modules
-
bytes
Search for regex matches in
&[u8]haystacks.
Functions
-
escape
Escapes all regular expression meta characters in
pattern.