Struct fancy_regex::Regex [−][src]
pub struct Regex { /* fields omitted */ }Expand description
A compiled regular expression.
Implementations
impl Regex[src]
impl Regex[src]pub fn new(re: &str) -> Result<Regex>[src]
pub fn new(re: &str) -> Result<Regex>[src]Parse and compile a regex with default options, see RegexBuilder.
Returns an Error if the pattern could not be parsed.
pub fn is_match(&self, text: &str) -> Result<bool>[src]
pub fn is_match(&self, text: &str) -> Result<bool>[src]Check if the regex matches the input text.
Example
Test if some text contains the same word twice:
let re = Regex::new(r"(\w+) \1").unwrap(); assert!(re.is_match("mirror mirror on the wall").unwrap());
pub fn find_iter<'r, 't>(&'r self, text: &'t str) -> Matches<'r, 't>ⓘ[src]
pub fn find_iter<'r, 't>(&'r self, text: &'t str) -> Matches<'r, 't>ⓘ[src]Returns an iterator for each successive non-overlapping match in text.
If you have capturing groups in your regex that you want to extract, use the Regex::captures_iter() method.
Example
Find all words followed by an exclamation point:
let re = Regex::new(r"\w+(?=!)").unwrap(); let mut matches = re.find_iter("so fancy! even with! iterators!"); assert_eq!(matches.next().unwrap().unwrap().as_str(), "fancy"); assert_eq!(matches.next().unwrap().unwrap().as_str(), "with"); assert_eq!(matches.next().unwrap().unwrap().as_str(), "iterators"); assert!(matches.next().is_none());
pub fn find<'t>(&self, text: &'t str) -> Result<Option<Match<'t>>>[src]
pub fn find<'t>(&self, text: &'t str) -> Result<Option<Match<'t>>>[src]Find the first match in the input text.
If you have capturing groups in your regex that you want to extract, use the Regex::captures() method.
Example
Find a word that is followed by an exclamation point:
let re = Regex::new(r"\w+(?=!)").unwrap(); assert_eq!(re.find("so fancy!").unwrap().unwrap().as_str(), "fancy");
pub fn find_from_pos<'t>(
&self,
text: &'t str,
pos: usize
) -> Result<Option<Match<'t>>>[src]
pub fn find_from_pos<'t>(
&self,
text: &'t str,
pos: usize
) -> Result<Option<Match<'t>>>[src]Returns the first match in text, starting from the specified byte position pos.
Examples
Finding match starting at a position:
let re = Regex::new(r"(?m:^)(\d+)").unwrap(); let text = "1 test 123\n2 foo"; let mat = re.find_from_pos(text, 7).unwrap().unwrap(); assert_eq!(mat.start(), 11); assert_eq!(mat.end(), 12);
Note that in some cases this is not the same as using the find
method and passing a slice of the string, see Regex::captures_from_pos() for details.
pub fn captures_iter<'r, 't>(&'r self, text: &'t str) -> CaptureMatches<'r, 't>ⓘNotable traits for CaptureMatches<'r, 't>
impl<'r, 't> Iterator for CaptureMatches<'r, 't> type Item = Result<Captures<'t>>;[src]
pub fn captures_iter<'r, 't>(&'r self, text: &'t str) -> CaptureMatches<'r, 't>ⓘNotable traits for CaptureMatches<'r, 't>
impl<'r, 't> Iterator for CaptureMatches<'r, 't> type Item = Result<Captures<'t>>;[src]Returns an iterator over all the non-overlapping capture groups matched in text.
Examples
Finding all matches and capturing parts of each:
let re = Regex::new(r"(\d{4})-(\d{2})").unwrap(); let text = "It was between 2018-04 and 2020-01"; let mut all_captures = re.captures_iter(text); let first = all_captures.next().unwrap().unwrap(); assert_eq!(first.get(1).unwrap().as_str(), "2018"); assert_eq!(first.get(2).unwrap().as_str(), "04"); assert_eq!(first.get(0).unwrap().as_str(), "2018-04"); let second = all_captures.next().unwrap().unwrap(); assert_eq!(second.get(1).unwrap().as_str(), "2020"); assert_eq!(second.get(2).unwrap().as_str(), "01"); assert_eq!(second.get(0).unwrap().as_str(), "2020-01"); assert!(all_captures.next().is_none());
pub fn captures<'t>(&self, text: &'t str) -> Result<Option<Captures<'t>>>[src]
pub fn captures<'t>(&self, text: &'t str) -> Result<Option<Captures<'t>>>[src]Returns the capture groups for the first match in text.
If no match is found, then Ok(None) is returned.
Examples
Finding matches and capturing parts of the match:
let re = Regex::new(r"(\d{4})-(\d{2})-(\d{2})").unwrap(); let text = "The date was 2018-04-07"; let captures = re.captures(text).unwrap().unwrap(); assert_eq!(captures.get(1).unwrap().as_str(), "2018"); assert_eq!(captures.get(2).unwrap().as_str(), "04"); assert_eq!(captures.get(3).unwrap().as_str(), "07"); assert_eq!(captures.get(0).unwrap().as_str(), "2018-04-07");
pub fn captures_from_pos<'t>(
&self,
text: &'t str,
pos: usize
) -> Result<Option<Captures<'t>>>[src]
pub fn captures_from_pos<'t>(
&self,
text: &'t str,
pos: usize
) -> Result<Option<Captures<'t>>>[src]Returns the capture groups for the first match in text, starting from
the specified byte position pos.
Examples
Finding captures starting at a position:
let re = Regex::new(r"(?m:^)(\d+)").unwrap(); let text = "1 test 123\n2 foo"; let captures = re.captures_from_pos(text, 7).unwrap().unwrap(); let group = captures.get(1).unwrap(); assert_eq!(group.as_str(), "2"); assert_eq!(group.start(), 11); assert_eq!(group.end(), 12);
Note that in some cases this is not the same as using the captures
method and passing a slice of the string, see the capture that we get
when we do this:
let re = Regex::new(r"(?m:^)(\d+)").unwrap(); let text = "1 test 123\n2 foo"; let captures = re.captures(&text[7..]).unwrap().unwrap(); assert_eq!(captures.get(1).unwrap().as_str(), "123");
This matched the number “123” because it’s at the beginning of the text of the string slice.
pub fn captures_len(&self) -> usize[src]
pub fn captures_len(&self) -> usize[src]Returns the number of captures, including the implicit capture of the entire expression.
pub fn capture_names(&self) -> CaptureNames<'_>ⓘNotable traits for CaptureNames<'r>
impl<'r> Iterator for CaptureNames<'r> type Item = Option<&'r str>;[src]
pub fn capture_names(&self) -> CaptureNames<'_>ⓘNotable traits for CaptureNames<'r>
impl<'r> Iterator for CaptureNames<'r> type Item = Option<&'r str>;[src]Returns an iterator over the capture names.
Trait Implementations
Auto Trait Implementations
impl RefUnwindSafe for Regex
impl Send for Regex
impl Sync for Regex
impl Unpin for Regex
impl UnwindSafe for Regex
Blanket Implementations
impl<T> BorrowMut<T> for T where
T: ?Sized, [src]
impl<T> BorrowMut<T> for T where
T: ?Sized, [src]pub fn borrow_mut(&mut self) -> &mut T[src]
pub fn borrow_mut(&mut self) -> &mut T[src]Mutably borrows from an owned value. Read more
impl<T> ToOwned for T where
T: Clone, [src]
impl<T> ToOwned for T where
T: Clone, [src]type Owned = T
type Owned = TThe resulting type after obtaining ownership.
pub fn to_owned(&self) -> T[src]
pub fn to_owned(&self) -> T[src]Creates owned data from borrowed data, usually by cloning. Read more
pub fn clone_into(&self, target: &mut T)[src]
pub fn clone_into(&self, target: &mut T)[src]🔬 This is a nightly-only experimental API. (toowned_clone_into)
recently added
Uses borrowed data to replace owned data, usually by cloning. Read more