RegSet

Struct RegSet 

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

A RegSet allows you to compile multiple regular expressions and search for any of them in a single pass through the text. This is more efficient than searching with each regex individually but RegSet has to own them.

Implementations§

Source§

impl RegSet

Source

pub fn new(patterns: &[&str]) -> Result<RegSet, Error>

Create a new RegSet from a slice of pattern strings

All patterns will be compiled with default Regex options.

§Examples
use onig::RegSet;

let set = RegSet::new(&[r"\d+", r"[a-z]+", r"[A-Z]+"]).unwrap();
Source

pub fn with_options( patterns: &[&str], options: RegexOptions, ) -> Result<RegSet, Error>

Create a new RegSet from a slice of pattern strings with specified options

All patterns will be compiled with the specified Regex options.

§Examples
use onig::{RegSet, RegexOptions};

let set = RegSet::with_options(&[r"\d+", r"[a-z]+"], RegexOptions::REGEX_OPTION_CAPTURE_GROUP).unwrap();
Source

pub fn empty() -> Result<RegSet, Error>

Create an empty RegSet

Creates a new empty RegSet that contains no regular expressions. Patterns can be added later using the add_pattern method.

§Examples
use onig::RegSet;

let empty_set = RegSet::empty().unwrap();
assert_eq!(empty_set.len(), 0);
assert!(empty_set.is_empty());
Source

pub fn empty_with_options(options: RegexOptions) -> Result<RegSet, Error>

Create an empty RegSet with specified options

Creates a new empty RegSet that contains no regular expressions. Patterns added later will use the specified options.

§Examples
use onig::{RegSet, RegexOptions};

let empty_set = RegSet::empty_with_options(RegexOptions::REGEX_OPTION_CAPTURE_GROUP).unwrap();
assert_eq!(empty_set.len(), 0);
assert!(empty_set.is_empty());
Source

pub fn add_pattern(&mut self, pattern: &str) -> Result<usize, Error>

Adds a new compiled regex pattern to the end of the RegSet.

§Examples
use onig::RegSet;

let mut set = RegSet::empty().unwrap();
let idx = set.add_pattern(r"\d+").unwrap();
assert_eq!(idx, 0);
assert_eq!(set.len(), 1);

let idx2 = set.add_pattern(r"[a-z]+").unwrap();
assert_eq!(idx2, 1);
assert_eq!(set.len(), 2);
Source

pub fn replace_pattern( &mut self, index: usize, pattern: &str, ) -> Result<(), Error>

Replace a regex pattern at the specified index

§Examples
use onig::RegSet;

let mut set = RegSet::new(&[r"\d+", r"[a-z]+"]).unwrap();
set.replace_pattern(0, r"[A-Z]+").unwrap();

assert!(set.find("123").is_none());
assert!(set.find("ABC").is_some());
Source

pub fn len(&self) -> usize

Returns the number of regexes in the set

Source

pub fn is_empty(&self) -> bool

Returns true if the RegSet contains no regexes

Source

pub fn find(&self, text: &str) -> Option<(usize, usize)>

Find the first match of any regex in the set

Returns a tuple of (regex_index, match_position) if a match is found, or None if no match is found.

§Examples
use onig::RegSet;

let set = RegSet::new(&[r"\d+", r"[a-z]+"]).unwrap();
if let Some((regex_index, pos)) = set.find("hello123") {
    println!("Regex {} matched at position {}", regex_index, pos);
}
Source

pub fn find_with_options( &self, text: &str, lead: RegSetLead, options: SearchOptions, ) -> Option<(usize, usize)>

Find the first match of any regex in the set with custom options

§Examples
use onig::{RegSet, RegSetLead, SearchOptions};

let set = RegSet::new(&[r"\d+", r"[a-z]+"]).unwrap();
if let Some((regex_index, pos)) = set.find_with_options(
    "hello123",
    RegSetLead::Regex,
    SearchOptions::SEARCH_OPTION_NONE
) {
    println!("Regex {} matched at position {}", regex_index, pos);
}
Source

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

Find the first match of any regex in the set with full capture group information

Returns a tuple of (regex_index, captures) if a match is found, or None if no match is found.

§Examples
use onig::RegSet;

let set = RegSet::new(&[r"(\d+)", r"([a-z]+)"]).unwrap();
if let Some((regex_index, captures)) = set.captures("hello123") {
    println!("Regex {} matched", regex_index);
    println!("Full match: {:?}", captures.at(0));
    println!("First capture group: {:?}", captures.at(1));
}
Source

pub fn captures_with_options<'t>( &self, text: &'t str, from: usize, to: usize, lead: RegSetLead, options: SearchOptions, ) -> Option<(usize, Captures<'t>)>

Find the first match with full capture group information and encoding support

Returns a tuple of (regex_index, captures) if a match is found, or None if no match is found.

§Examples
use onig::{RegSet, RegSetLead, SearchOptions, EncodedBytes};

let set = RegSet::new(&[r"(\d+)", r"([a-z]+)"]).unwrap();
if let Some((regex_index, captures)) = set.captures_with_options(
    "hello123",
    0,
    8,
    RegSetLead::Position,
    SearchOptions::SEARCH_OPTION_NONE
) {
    println!("Regex {} matched", regex_index);
    println!("Full match: {:?}", captures.at(0));
    println!("First capture group: {:?}", captures.at(1));
}

Trait Implementations§

Source§

impl Debug for RegSet

Source§

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

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

impl Drop for RegSet

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl Send for RegSet

Source§

impl Sync for RegSet

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.