Struct fancy_regex::Regex [−][src]
A compiled regular expression.
Implementations
impl 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 as_str(&self) -> &str
[src]
Returns the original string of this regex.
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]
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]
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]
&self,
text: &'t str,
pos: usize
) -> Result<Option<Match<'t>>>
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]
Notable traits for CaptureMatches<'r, 't>
impl<'r, 't> Iterator for CaptureMatches<'r, 't> type Item = Result<Captures<'t>>;
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]
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]
&self,
text: &'t str,
pos: usize
) -> Result<Option<Captures<'t>>>
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]
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]
Notable traits for CaptureNames<'r>
impl<'r> Iterator for CaptureNames<'r> type Item = Option<&'r str>;
Returns an iterator over the capture names.
Trait Implementations
Auto Trait Implementations
impl RefUnwindSafe for Regex
[src]
impl Send for Regex
[src]
impl Sync for Regex
[src]
impl Unpin for Regex
[src]
impl UnwindSafe for Regex
[src]
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
pub fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,