Skip to main content

Regex

Struct Regex 

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

Lazily compiled regex instance. Uses Mutex for interior mutability.

Implementations§

Source§

impl Regex

Source

pub fn stream(&self, input: &[u8]) -> Result<Vec<Match>, Error>

Shortest matches, left-to-right. State resets after each match.

Source

pub fn stream_with<F: FnMut(Match)>( &self, input: &[u8], on_match: F, ) -> Result<(), Error>

Shortest matches; callback variant of Regex::stream.

Source

pub fn stream_first(&self, input: &[u8]) -> Result<Option<Match>, Error>

Shortest match starting earliest in input, or None.

Source§

impl Regex

Source

pub fn stream_ends(&self, input: &[u8]) -> Result<Vec<usize>, Error>

Shortest match ends only; skips the rev pass.

Source

pub fn stream_ends_with<F: FnMut(usize)>( &self, input: &[u8], on_match: F, ) -> Result<(), Error>

Shortest match ends; callback variant of Regex::stream_ends.

Source§

impl Regex

Source

pub const SEEK_INITIAL: u32 = 0

Initial state for Regex::seek_fwd / Regex::seek_rev cursors.

Source

pub fn stream_chunk<F: FnMut(usize)>( &self, chunk: &[u8], state: StreamState, on_match: F, ) -> Result<StreamState, Error>

Feed one chunk, resuming from state. Emits shortest match ends in absolute offsets. Pass StreamState::new() for the first chunk.

Source

pub fn seek_fwd( &self, input: &[u8], state: u32, pos: usize, ) -> Result<Option<(u32, usize)>, Error>

Forward cursor scan; returns the next shortest match end as (resume_state, end). First call: state = SEEK_INITIAL, pos = 0. Subsequent calls: pass back the returned values.

Source

pub fn seek_rev( &self, input: &[u8], state: u32, pos: usize, ) -> Result<Option<(u32, usize)>, Error>

Reverse cursor scan over input[..pos] using rev LDFA on _*·rev(node). Returns the next shortest match start (rightmost-first) as (resume_state, start). First call: state = SEEK_INITIAL, pos = input.len().

Source§

impl Regex

Source

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

compile a pattern with default options.

let re = resharp::Regex::new(r"\b\w+\b").unwrap();
Source

pub fn with_options(pattern: &str, opts: RegexOptions) -> Result<Regex, Error>

compile a pattern with custom RegexOptions.

use resharp::{Regex, RegexOptions};

let re = Regex::with_options(
    r"hello",
    RegexOptions::default().case_insensitive(true),
).unwrap();
assert!(re.is_match(b"HELLO").unwrap());
Source

pub fn find_all(&self, input: &[u8]) -> Result<Vec<Match>, Error>

all non-overlapping leftmost-first matches as [start, end) byte ranges.

let re = resharp::Regex::new(r"\d+").unwrap();
let m = re.find_all(b"abc 123 def 456").unwrap();
assert_eq!(m.len(), 2);
assert_eq!((m[0].start, m[0].end), (4, 7));
Source§

impl Regex

Source

pub fn find_anchored(&self, input: &[u8]) -> Result<Option<Match>, Error>

longest match anchored at position 0.

returns None if the pattern does not match at position 0.

Source

pub fn is_match(&self, input: &[u8]) -> Result<bool, Error>

whether the pattern matches anywhere in the input.

faster than find_all when you only need a yes/no answer.

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