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
impl RegSet
Sourcepub fn new(patterns: &[&str]) -> Result<RegSet, Error>
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();Sourcepub fn with_options(
patterns: &[&str],
options: RegexOptions,
) -> Result<RegSet, Error>
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();Sourcepub fn empty() -> Result<RegSet, Error>
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());Sourcepub fn empty_with_options(options: RegexOptions) -> Result<RegSet, Error>
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());Sourcepub fn add_pattern(&mut self, pattern: &str) -> Result<usize, Error>
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);Sourcepub fn replace_pattern(
&mut self,
index: usize,
pattern: &str,
) -> Result<(), Error>
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());Sourcepub fn find(&self, text: &str) -> Option<(usize, usize)>
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);
}Sourcepub fn find_with_options(
&self,
text: &str,
lead: RegSetLead,
options: SearchOptions,
) -> Option<(usize, usize)>
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);
}Sourcepub fn captures<'t>(&self, text: &'t str) -> Option<(usize, Captures<'t>)>
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));
}Sourcepub fn captures_with_options<'t>(
&self,
text: &'t str,
from: usize,
to: usize,
lead: RegSetLead,
options: SearchOptions,
) -> Option<(usize, Captures<'t>)>
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));
}