Skip to main content

Regex

Struct Regex 

Source
pub struct Regex { /* private fields */ }
Expand description

A compiled pattern.

Implementations§

Source§

impl Regex

Source

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).

Source

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.

Source

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.

Source

pub fn as_str(&self) -> &str

The original pattern string.

Source

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.

Source

pub fn capture_names(&self) -> impl Iterator<Item = Option<&str>>

The name of each capture group (group 0 first), None for the unnamed ones.

Source

pub fn is_match(&self, text: &str) -> bool

Whether the pattern matches anywhere in text.

Source

pub fn is_match_at(&self, text: &str, start: usize) -> bool

Like is_match, searching from byte offset start.

Source

pub fn find<'t>(&self, text: &'t str) -> Option<Match<'t>>

The leftmost match’s whole-match span, or None.

Source

pub fn find_at<'t>(&self, text: &'t str, start: usize) -> Option<Match<'t>>

Like find, searching from byte offset start.

Source

pub fn find_iter<'r, 't>(&'r self, text: &'t str) -> Matches<'r, 't>

Iterate the non-overlapping whole-match spans in text.

Source

pub fn captures<'t>(&self, text: &'t str) -> Option<Captures<'t>>

The capture groups of the leftmost match, or None.

Source

pub fn captures_at<'t>( &self, text: &'t str, start: usize, ) -> Option<Captures<'t>>

Like captures, searching from byte offset start.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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

Source

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).

Source

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.

Source

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).

Source

pub fn split<'r, 't>(&'r self, text: &'t str) -> Split<'r, 't>

Iterate the substrings of text delimited by matches (leading/trailing empties included), mirroring regex::Regex::split.

Source

pub fn splitn<'r, 't>(&'r self, text: &'t str, limit: usize) -> SplitN<'r, 't>

Like split, but yielding at most limit substrings (the last is the unsplit remainder). limit == 0 yields nothing.

Trait Implementations§

Source§

impl Debug for Regex

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Drop for Regex

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more
Source§

impl Send for Regex

Source§

impl Sync for Regex

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.