pub struct Pattern { /* private fields */ }Expand description
Main ReXile pattern type
Implementations§
Source§impl Pattern
impl Pattern
pub fn new(pattern: &str) -> Result<Self, PatternError>
pub fn is_match(&self, text: &str) -> bool
pub fn find(&self, text: &str) -> Option<(usize, usize)>
pub fn find_all(&self, text: &str) -> Vec<(usize, usize)>
Sourcepub fn find_iter<'a>(&'a self, text: &'a str) -> FindIter<'a> ⓘ
pub fn find_iter<'a>(&'a self, text: &'a str) -> FindIter<'a> ⓘ
Create an iterator over all matches
Sourcepub fn captures<'t>(&self, text: &'t str) -> Option<Captures<'t>>
pub fn captures<'t>(&self, text: &'t str) -> Option<Captures<'t>>
Capture groups from the first match
Returns a Captures object if the pattern matches, containing the full match
and any captured groups. Returns None if no match is found.
§Example
use rexile::Pattern;
let pattern = Pattern::new(r"(\w+)@(\w+)\.(\w+)").unwrap();
if let Some(caps) = pattern.captures("email: test@example.com") {
println!("Full: {}", &caps[0]); // test@example.com
println!("User: {}", &caps[1]); // test
println!("Domain: {}", &caps[2]); // example
println!("TLD: {}", &caps[3]); // com
}Sourcepub fn captures_iter<'r, 't>(&'r self, text: &'t str) -> CapturesIter<'r, 't> ⓘ
pub fn captures_iter<'r, 't>(&'r self, text: &'t str) -> CapturesIter<'r, 't> ⓘ
Iterate over all captures in the text
Returns an iterator that yields Captures for each match found.
§Example
use rexile::Pattern;
let pattern = Pattern::new(r"(\w+)=(\d+)").unwrap();
for caps in pattern.captures_iter("a=1 b=2 c=3") {
println!("{} = {}", &caps[1], &caps[2]);
}Sourcepub fn replace(&self, text: &str, replacement: &str) -> String
pub fn replace(&self, text: &str, replacement: &str) -> String
Replace the first match with a replacement string
Supports capture group references using $1, $2, etc.
§Example
use rexile::Pattern;
let pattern = Pattern::new(r"(\w+)").unwrap();
let result = pattern.replace("hello world", "goodbye");
assert_eq!(result, "goodbye world");
// With captures
let pattern = Pattern::new(r"(\w+)=(\d+)").unwrap();
let result = pattern.replace("a=1 b=2", "$1:[$2]");
assert_eq!(result, "a:[1] b=2");Sourcepub fn replace_all(&self, text: &str, replacement: &str) -> String
pub fn replace_all(&self, text: &str, replacement: &str) -> String
Replace all matches with a replacement string
Supports capture group references using $1, $2, etc.
§Example
use rexile::Pattern;
let pattern = Pattern::new(r"(\w+)=(\d+)").unwrap();
let result = pattern.replace_all("a=1 b=2", "$1:[$2]");
assert_eq!(result, "a:[1] b:[2]");Trait Implementations§
Auto Trait Implementations§
impl Freeze for Pattern
impl RefUnwindSafe for Pattern
impl Send for Pattern
impl Sync for Pattern
impl Unpin for Pattern
impl UnwindSafe for Pattern
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more