Regex

Struct Regex 

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

NFA regex engine

Implementations§

Source§

impl Regex

Implementing regex methods or functions

Source

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

Creates new regex engine given the infix regex pattern.

§Errors

Returns descriptive error message string in case of failure which are categorized as:

  1. Syntax Error: Syntax error in the regex pattern. E.g. [[ab]]] (Syntax Error: Nested square brackets ‘[]’ not allowed.)
  2. Parse Error: Error occured during parsing the postfix regex expression. E.g. [ab]+? (Parse Error: Cannot apply repeat or duplication operator twice.)
§Examples
let mut r = nfa_regex::Regex::new("[a-z]").unwrap();
assert_eq!(r.match_pattern("a"), true);
Source

pub fn match_pattern(&mut self, s: &str) -> bool

Matches input string to NFA regex pattern. Returns true if complete string matches the pattern, false otherwise.

§Examples
let mut r = nfa_regex::Regex::new("[a-z]+").unwrap();
assert_eq!(r.match_pattern("ashutosh"), true);
assert_eq!(r.match_pattern("[om, ashutosh]"), false);
assert_eq!(r.match_pattern(""), false);
Source

pub fn search_pattern_and_get_index(&mut self, s: &str) -> (usize, usize)

Searches the pattern inside string and returns the first match’s begin and end index into the input string, otherwise returns begin and end index as 0.

§Examples
let mut r = nfa_regex::Regex::new("[a-z]+").unwrap();
assert_eq!(r.search_pattern_and_get_index("ashutosh"), (0, 8));
assert_eq!(r.search_pattern_and_get_index("[om, ashutosh, 3.141593]"), (1, 3));
assert_eq!(r.search_pattern_and_get_index(""), (0, 0));
Source

pub fn search_pattern_and_get_slice<'a>( &mut self, s: &'a str, ) -> (&'a str, &'a str, &'a str)

Search the substring inside given string which matches the regex pattern and return unmatched prefix, matched and unmatched suffix string slices.

§Examples
let mut r = nfa_regex::Regex::new("[a-z]+").unwrap();
assert_eq!(r.search_pattern_and_get_slice("ashutosh"), ("", "ashutosh", ""));
assert_eq!(r.search_pattern_and_get_slice("[om, ashutosh, 3.141593]"), ("[", "om", ", ashutosh, 3.141593]"));
assert_eq!(r.search_pattern_and_get_slice(""), ("", "", ""));
Source

pub fn search_all_patterns_and_get_index_list( &mut self, s: &str, ) -> Vec<(usize, usize)>

Search all substrings inside given string which matches regex pattern. Return the list of begin-end indices for input string of all matched pattern.

§Examples
let mut r = nfa_regex::Regex::new("[a-z]+").unwrap();
assert_eq!(r.search_all_patterns_and_get_index_list("ashutosh"), vec![(0, 8)]);
assert_eq!(r.search_all_patterns_and_get_index_list("[om, ashutosh, 3.141593]"), vec![(1, 3), (5, 13)]);
assert_eq!(r.search_all_patterns_and_get_index_list(""), vec![]);
Source

pub fn replace_pattern(&mut self, target: &mut String, src: &str)

Replace the occurance of first match of regex pattern to substring inside given input string with another given source string.

§Examples
let mut r = nfa_regex::Regex::new("[a-z]+").unwrap();
let mut s1 = String::from("ashutosh");
let mut s2 = String::from("[om, ashutosh, 3.141593]");
r.replace_pattern(&mut s1, "MAHAKALI");
r.replace_pattern(&mut s2, "MAHAKALI");
assert_eq!(s1, "MAHAKALI");
assert_eq!(s2, "[MAHAKALI, ashutosh, 3.141593]");
Source

pub fn replace_all_patterns(&mut self, target: &mut String, src: &str)

Replaces all occurances of substring matching regex pattern inside given input string with given source string. Returns unchanged string if no matched substring found.

§Examples
let mut r = nfa_regex::Regex::new("[a-z]+").unwrap();
let mut s = String::from("[om, ashutosh, 3.141593]");
r.replace_all_patterns(&mut s, "MAHAKALI");
assert_eq!(s, "[MAHAKALI, MAHAKALI, 3.141593]");

Trait Implementations§

Source§

impl Clone for Regex

Source§

fn clone(&self) -> Regex

Returns a duplicate of the value. Read more
1.0.0 · 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 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 = 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.