Struct fancy_regex::Regex[][src]

pub struct Regex { /* fields omitted */ }
Expand description

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>

Notable traits for Matches<'r, 't>

impl<'r, 't> Iterator for Matches<'r, 't> type Item = Result<Match<'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]

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]

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]

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]

Returns an iterator over the capture names.

Trait Implementations

impl Clone for Regex[src]

fn clone(&self) -> Regex[src]

Returns a copy of the value. Read more

fn clone_from(&mut self, source: &Self)1.0.0[src]

Performs copy-assignment from source. Read more

impl Debug for Regex[src]

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

Shows the original regular expression.

impl Display for Regex[src]

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

Shows the original regular expression

impl FromStr for Regex[src]

fn from_str(s: &str) -> Result<Regex>[src]

Attempts to parse a string into a regular expression

type Err = Error

The associated error which can be returned from parsing.

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> Any for T where
    T: 'static + ?Sized
[src]

pub fn type_id(&self) -> TypeId[src]

Gets the TypeId of self. Read more

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

pub fn borrow(&self) -> &T[src]

Immutably borrows from an owned value. Read more

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

pub fn borrow_mut(&mut self) -> &mut T[src]

Mutably borrows from an owned value. Read more

impl<T> From<T> for T[src]

pub fn from(t: T) -> T[src]

Performs the conversion.

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

pub fn into(self) -> U[src]

Performs the conversion.

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

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]

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

Uses borrowed data to replace owned data, usually by cloning. Read more

impl<T> ToString for T where
    T: Display + ?Sized
[src]

pub default fn to_string(&self) -> String[src]

Converts the given value to a String. Read more

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

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]

Performs the conversion.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

pub fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>[src]

Performs the conversion.