pub struct Regex(_);
Expand description
A compiled regular expression for matching Unicode strings.
Implementations
sourceimpl Regex
impl Regex
Core regular expression methods.
sourcepub fn new<S: Into<String>>(re: S) -> Result<Regex>
pub fn new<S: Into<String>>(re: S) -> Result<Regex>
Compiles a regular expression. Once compiled, it can be used repeatedly to search, split or replace text in a string.
If an invalid expression is given, then an error is returned.
sourcepub fn is_match(&self, text: &str) -> bool
pub fn is_match(&self, text: &str) -> bool
Returns true if and only if the regex matches the string given.
It is recommended to use this method if all you need to do is test a match, since the underlying matching engine may be able to do less work.
Examples
Test if some text contains at least one word with exactly 13 Unicode word characters:
let text = "I categorically deny having triskaidekaphobia.";
assert!(Regex::new(r"\b\w{13}\b").unwrap().is_match(text));
sourcepub fn find<'t>(&self, text: &'t str) -> Option<Match<'t>>
pub fn find<'t>(&self, text: &'t str) -> Option<Match<'t>>
Returns the start and end byte range of the leftmost-first match in text. If no match exists, then None is returned.
Note that this should only be used if you want to discover the position of the match. Testing the existence of a match is faster if you use is_match.
Examples
Find the start and end location of the first word with exactly 13 Unicode word characters:
let text = "I categorically deny having triskaidekaphobia.";
let mat = Regex::new(r"\b\w{13}\b").unwrap().find(text).unwrap();
assert_eq!(mat.start(), 2);
assert_eq!(mat.end(), 15);
sourcepub fn find_iter<'t>(&self, text: &'t str) -> Matches<'t>
pub fn find_iter<'t>(&self, text: &'t str) -> Matches<'t>
Returns an iterator for each successive non-overlapping match in
text
, returning the start and end byte indices with respect to
text
.
Examples
Find the start and end location of every word with exactly 13 Unicode word characters:
let text = "Retroactively relinquishing remunerations is reprehensible.";
for mat in Regex::new(r"\b\w{13}\b").unwrap().find_iter(text) {
println!("{:?}", mat);
}
sourcepub fn split<'t>(&self, text: &'t str) -> Split<'t>
pub fn split<'t>(&self, text: &'t str) -> Split<'t>
Returns an iterator of substrings of text
delimited by a match of the
regular expression. Namely, each element of the iterator corresponds to
text that isn’t matched by the regular expression.
This method will not copy the text given.
Examples
To split a string delimited by arbitrary amounts of spaces or tabs:
let re = Regex::new(r"[ \t]+").unwrap();
let fields: Vec<&str> = re.split("a b \t c\td e").collect();
assert_eq!(fields, vec!["a", "b", "c", "d", "e"]);
sourcepub fn splitn<'t>(&self, text: &'t str, limit: usize) -> SplitN<'t>
pub fn splitn<'t>(&self, text: &'t str, limit: usize) -> SplitN<'t>
Returns an iterator of at most limit
substrings of text
delimited
by a match of the regular expression. (A limit
of 0
will return no
substrings.) Namely, each element of the iterator corresponds to text
that isn’t matched by the regular expression. The remainder of the
string that is not split will be the last element in the iterator.
This method will not copy the text given.
Examples
Get the first two words in some text:
let re = Regex::new(r"\W+").unwrap();
let fields: Vec<&str> = re.splitn("Hey! How are you?", 3).collect();
assert_eq!(fields, vec!("Hey", "How", "are you?"));