pub struct Regex { /* private fields */ }Expand description
A compiled pattern.
Implementations§
Source§impl Regex
impl Regex
Sourcepub fn new(pattern: &str) -> Result<Regex, Error>
pub fn new(pattern: &str) -> Result<Regex, Error>
Compile pattern. Returns the engine’s error message if the pattern is invalid or cannot be run
linearly (the strict policy).
Sourcepub fn with_flags(pattern: &str, flags: u32) -> Result<Regex, Error>
pub fn with_flags(pattern: &str, flags: u32) -> Result<Regex, Error>
Compile with a real::flags bitmask (icase=1, multiline=2, dotall=4, bytes=8, verbose=16, ecma=32,
ascii=64). Prefer RegexBuilder for readable options.
Sourcepub fn engine(&self) -> Engine
pub fn engine(&self) -> Engine
Which engine backs this pattern — Engine::Real (linear, ReDoS-safe) or Engine::Fallback (the
regex crate, when the fallback feature delegated it). Always Real unless the feature is used.
Sourcepub fn captures_len(&self) -> usize
pub fn captures_len(&self) -> usize
The number of capture slots, including the implicit whole-match group 0 (so always >= 1) — the regex crate’s convention.
Sourcepub fn capture_names(&self) -> impl Iterator<Item = Option<&str>>
pub fn capture_names(&self) -> impl Iterator<Item = Option<&str>>
The name of each capture group (group 0 first), None for the unnamed ones.
Sourcepub fn is_match_at(&self, text: &str, start: usize) -> bool
pub fn is_match_at(&self, text: &str, start: usize) -> bool
Like is_match, searching from byte offset start.
Sourcepub fn find<'t>(&self, text: &'t str) -> Option<Match<'t>>
pub fn find<'t>(&self, text: &'t str) -> Option<Match<'t>>
The leftmost match’s whole-match span, or None.
Sourcepub fn find_at<'t>(&self, text: &'t str, start: usize) -> Option<Match<'t>>
pub fn find_at<'t>(&self, text: &'t str, start: usize) -> Option<Match<'t>>
Like find, searching from byte offset start.
Sourcepub fn find_iter<'r, 't>(&'r self, text: &'t str) -> Matches<'r, 't> ⓘ
pub fn find_iter<'r, 't>(&'r self, text: &'t str) -> Matches<'r, 't> ⓘ
Iterate the non-overlapping whole-match spans in text.
Sourcepub fn captures<'t>(&self, text: &'t str) -> Option<Captures<'t>>
pub fn captures<'t>(&self, text: &'t str) -> Option<Captures<'t>>
The capture groups of the leftmost match, or None.
Sourcepub fn captures_at<'t>(
&self,
text: &'t str,
start: usize,
) -> Option<Captures<'t>>
pub fn captures_at<'t>( &self, text: &'t str, start: usize, ) -> Option<Captures<'t>>
Like captures, searching from byte offset start.
Sourcepub fn capture_locations(&self) -> CaptureLocations
pub fn capture_locations(&self) -> CaptureLocations
A reusable capture-slot buffer for this pattern — drop-in for
[regex::Regex::capture_locations]. Pair with captures_read
to extract groups without allocating a Captures per match.
Sourcepub fn captures_read<'t>(
&self,
locs: &mut CaptureLocations,
text: &'t str,
) -> Option<Match<'t>>
pub fn captures_read<'t>( &self, locs: &mut CaptureLocations, text: &'t str, ) -> Option<Match<'t>>
Fill locs with the leftmost match’s group spans (no per-match allocation). Returns the
whole-match Match span, or None. Mirrors regex::Regex::captures_read.
Sourcepub fn captures_read_at<'t>(
&self,
locs: &mut CaptureLocations,
text: &'t str,
start: usize,
) -> Option<Match<'t>>
pub fn captures_read_at<'t>( &self, locs: &mut CaptureLocations, text: &'t str, start: usize, ) -> Option<Match<'t>>
Like captures_read, searching from byte offset start.
Sourcepub fn captures_read_iter<'r, 't>(
&'r self,
text: &'t str,
) -> CaptureLocationMatches<'r, 't> ⓘ
pub fn captures_read_iter<'r, 't>( &'r self, text: &'t str, ) -> CaptureLocationMatches<'r, 't> ⓘ
Iterate non-overlapping matches without allocating a Captures per match.
Yields the whole-match Match; after each step, read groups with
CaptureLocationMatches::get (or copy into a CaptureLocations via
CaptureLocationMatches::read_captures). Prefer this over
captures_iter in capture-dense hot loops.
Sourcepub fn captures_iter<'r, 't>(&'r self, text: &'t str) -> CaptureMatches<'r, 't> ⓘ
pub fn captures_iter<'r, 't>(&'r self, text: &'t str) -> CaptureMatches<'r, 't> ⓘ
Iterate the capture groups of each non-overlapping match in text.
Sourcepub fn shortest_match(&self, text: &str) -> Option<usize>
pub fn shortest_match(&self, text: &str) -> Option<usize>
The end offset of the leftmost match (a match exists iff this is Some). Divergence: like the
regex crate, REAL is leftmost-first, but this returns the leftmost match’s greedy end, whereas
the regex crate returns the earliest position at which a match completes (e.g. a+ on "aaa": REAL
3, regex 1). A true earliest-completion mode is a parked follow-up (a first-accept stop in the
forward pass). Use this as an is_match that also reports where the leftmost match ends.
Sourcepub fn count_matches(&self, text: &str) -> usize
pub fn count_matches(&self, text: &str) -> usize
Count non-overlapping matches without materialising match objects (matching-only).
Prefer this over counting find_iter when only the count matters, and for
trailing-lookahead class+ patterns where the fast path lives here (not on find_iter). Parity:
re.count_matches(t) == re.find_iter(t).count().
Source§impl Regex
impl Regex
Sourcepub fn replace<'t, R: Replacer>(&self, text: &'t str, rep: R) -> Cow<'t, str>
pub fn replace<'t, R: Replacer>(&self, text: &'t str, rep: R) -> Cow<'t, str>
Replace the leftmost match in text with rep. If there is no match, text is returned unchanged
(borrowed).
Sourcepub fn replace_all<'t, R: Replacer>(
&self,
text: &'t str,
rep: R,
) -> Cow<'t, str>
pub fn replace_all<'t, R: Replacer>( &self, text: &'t str, rep: R, ) -> Cow<'t, str>
Replace every non-overlapping match in text with rep.
Sourcepub fn replacen<'t, R: Replacer>(
&self,
text: &'t str,
limit: usize,
rep: R,
) -> Cow<'t, str>
pub fn replacen<'t, R: Replacer>( &self, text: &'t str, limit: usize, rep: R, ) -> Cow<'t, str>
Replace at most limit matches (0 means all).