Skip to main content

Regex

Struct Regex 

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

A compiled regex. Clones share the native pattern and may match concurrently.

Implementations§

Source§

impl Regex

Source

pub fn new(pattern: &str) -> Result<Self, Error>

Compiles a pattern using the default options.

Source

pub fn as_str(&self) -> &str

Returns the original pattern.

Source

pub fn captures_len(&self) -> usize

Returns the number of capture groups, including group zero.

Source

pub fn is_match(&self, subject: &[u8]) -> bool

Returns whether the regex matches anywhere in the input.

Source

pub fn is_match_at(&self, subject: &[u8], start: usize) -> bool

Searches from a byte offset while preserving anchor and lookbehind context. An offset beyond the input returns false.

Source

pub fn is_full_match(&self, subject: &[u8]) -> bool

Returns whether the regex matches the entire input.

Source

pub fn find<'s>(&self, subject: &'s [u8]) -> Option<Match<'s>>

Finds the first match in the input.

Source

pub fn find_at<'s>(&self, subject: &'s [u8], start: usize) -> Option<Match<'s>>

Finds the first match at or after a byte offset, preserving input context. An offset beyond the input returns None.

Source

pub fn captures<'s>(&self, subject: &'s [u8]) -> Option<Captures<'s>>

Returns the capture groups of the first match.

Source

pub fn captures_at<'s>( &self, subject: &'s [u8], start: usize, ) -> Option<Captures<'s>>

Returns captures for the first match at or after a byte offset. Input context is preserved; offsets beyond the input return None.

Source

pub fn scan<'s, F, B>(&self, subject: &'s [u8], callback: F) -> ControlFlow<B>
where F: FnMut(Match<'s>) -> ControlFlow<B>,

Visits non-overlapping matches in leftmost-first order.

Return ControlFlow::Continue(()) to continue or ControlFlow::Break(value) to stop and return that value. Empty matches are included: after one, nonempty alternatives at the same position are tried before advancing. Callbacks may perform nested scans. Callback panics propagate after the native scan has stopped, without unwinding through C++.

use std::ops::ControlFlow;
use javelin_pattern::bytes::Regex;
let re = Regex::new(r"\d+")?;
let mut ranges = Vec::new();
let result = re.scan(b"12 and 34", |m| {
    ranges.push(m.range());
    ControlFlow::<()>::Continue(())
});
assert_eq!(result, ControlFlow::Continue(()));
assert_eq!(ranges, [0..2, 7..9]);
Source

pub fn scan_at<'s, F, B>( &self, subject: &'s [u8], start: usize, callback: F, ) -> ControlFlow<B>
where F: FnMut(Match<'s>) -> ControlFlow<B>,

Like Regex::scan, starting at a byte offset into the original input. An offset beyond the input completes without invoking the callback.

Source

pub fn scan_captures<'s, F, B>( &self, subject: &'s [u8], callback: F, ) -> ControlFlow<B>
where F: FnMut(Captures<'s>) -> ControlFlow<B>,

Visits capture groups with the iteration and stopping rules of Regex::scan. Capture ranges are copied into Rust-owned storage for each match.

Source

pub fn scan_captures_at<'s, F, B>( &self, subject: &'s [u8], start: usize, callback: F, ) -> ControlFlow<B>
where F: FnMut(Captures<'s>) -> ControlFlow<B>,

Like Regex::scan_captures, starting at a byte offset into the original input. An offset beyond the input completes without invoking the callback.

Trait Implementations§

Source§

impl Clone for Regex

Source§

fn clone(&self) -> Regex

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Regex

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl Freeze for Regex

§

impl RefUnwindSafe for Regex

§

impl Send for Regex

§

impl Sync for Regex

§

impl Unpin for Regex

§

impl UnsafeUnpin for Regex

§

impl UnwindSafe for Regex

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.