pub struct Regex { /* private fields */ }Expand description
A Regex is the compiled version of a pattern.
Implementations§
Source§impl Regex
impl Regex
Sourcepub fn new(pattern: &str) -> Result<Regex, Error>
pub fn new(pattern: &str) -> Result<Regex, Error>
Construct a regex by parsing pattern using the default flags.
An Error may be returned if the syntax is invalid.
Note that this is rather expensive; prefer to cache a Regex which is
intended to be used more than once.
Sourcepub fn with_flags<F>(pattern: &str, flags: F) -> Result<Regex, Error>
pub fn with_flags<F>(pattern: &str, flags: F) -> Result<Regex, Error>
Construct a regex by parsing pattern with flags.
An Error may be returned if the syntax is invalid.
Note it is preferable to cache a Regex which is intended to be used more
than once, as the parse may be expensive. For example:
Sourcepub fn from_unicode<I, F>(pattern: I, flags: F) -> Result<Regex, Error>
pub fn from_unicode<I, F>(pattern: I, flags: F) -> Result<Regex, Error>
Construct a regex by parsing pattern with flags, where
pattern is an iterator of u32 Unicode codepoints.
An Error may be returned if the syntax is invalid.
This allows parsing regular expressions from exotic strings in
other encodings, such as UTF-16 or UTF-32.
Sourcepub fn find_iter<'r, 't>(&'r self, text: &'t str) -> Matches<'r, 't>
pub fn find_iter<'r, 't>(&'r self, text: &'t str) -> Matches<'r, 't>
Searches text, returning an iterator over non-overlapping matches.
Note that the resulting Iterator borrows both the regex 'r and the
input string as 't.
Sourcepub fn find_from<'r, 't>(
&'r self,
text: &'t str,
start: usize,
) -> Matches<'r, 't>
pub fn find_from<'r, 't>( &'r self, text: &'t str, start: usize, ) -> Matches<'r, 't>
Returns an iterator for matches found in ‘text’ starting at byte index
start. Note this may be different from passing a sliced text in
the case of lookbehind assertions.
Example:
use regress::Regex;
let text = "xyxy";
let re = Regex::new(r"(?<=x)y").unwrap();
let t1 = re.find(&text[1..]).unwrap().range();
assert!(t1 == (2..3));
let t2 = re.find_from(text, 1).next().unwrap().range();
assert!(t2 == (1..2));Sourcepub fn find_ascii(&self, text: &str) -> Option<Match>
pub fn find_ascii(&self, text: &str) -> Option<Match>
Searches text to find the first match.
The input text is expected to be ascii-only: only ASCII case-folding is
supported.
Sourcepub fn find_iter_ascii<'r, 't>(&'r self, text: &'t str) -> AsciiMatches<'r, 't>
pub fn find_iter_ascii<'r, 't>(&'r self, text: &'t str) -> AsciiMatches<'r, 't>
Searches text, returning an iterator over non-overlapping matches.
The input text is expected to be ascii-only: only ASCII case-folding is
supported.
Sourcepub fn find_from_ascii<'r, 't>(
&'r self,
text: &'t str,
start: usize,
) -> AsciiMatches<'r, 't>
pub fn find_from_ascii<'r, 't>( &'r self, text: &'t str, start: usize, ) -> AsciiMatches<'r, 't>
Returns an iterator for matches found in ‘text’ starting at byte index
start.
Sourcepub fn replace(&self, text: &str, replacement: &str) -> String
pub fn replace(&self, text: &str, replacement: &str) -> String
Replaces the first match of the regex in text with the replacement string.
The replacement string may contain capture group references in the form $1, $2, etc.,
where $1 refers to the first capture group, $2 to the second, and so on.
$0 refers to the entire match. Use $$ to insert a literal $.
If no match is found, the original text is returned unchanged.
§Examples
use regress::Regex;
let re = Regex::new(r"(\w+)\s+(\w+)").unwrap();
let result = re.replace("hello world", "$2 $1");
assert_eq!(result, "world hello");
let re = Regex::new(r"(\d{4})-(\d{2})-(\d{2})").unwrap();
let result = re.replace("2023-12-25", "$2/$3/$1");
assert_eq!(result, "12/25/2023");Sourcepub fn replace_all(&self, text: &str, replacement: &str) -> String
pub fn replace_all(&self, text: &str, replacement: &str) -> String
Replaces all matches of the regex in text with the replacement string.
The replacement string may contain capture group references in the form $1, $2, etc.,
where $1 refers to the first capture group, $2 to the second, and so on.
$0 refers to the entire match. Use $$ to insert a literal $.
§Examples
use regress::Regex;
let re = Regex::new(r"(\w+)\s+(\w+)").unwrap();
let result = re.replace_all("hello world foo bar", "$2-$1");
assert_eq!(result, "world-hello bar-foo");
let re = Regex::new(r"\b(\w)(\w+)").unwrap();
let result = re.replace_all("hello world", "$1.$2");
assert_eq!(result, "h.ello w.orld");Sourcepub fn replace_with<F>(&self, text: &str, replacement: F) -> String
pub fn replace_with<F>(&self, text: &str, replacement: F) -> String
Replaces the first match of the regex in text using a closure.
The closure receives a &Match and should return the replacement string.
This is useful for dynamic replacements that depend on the match details.
If no match is found, the original text is returned unchanged.
§Examples
use regress::Regex;
let re = Regex::new(r"\d+").unwrap();
let text = "Price: $123";
let result = re.replace_with(text, |m| {
let num: i32 = m.as_str(text).parse().unwrap();
format!("{}", num * 2)
});
assert_eq!(result, "Price: $246");Sourcepub fn replace_all_with<F>(&self, text: &str, replacement: F) -> String
pub fn replace_all_with<F>(&self, text: &str, replacement: F) -> String
Replaces all matches of the regex in text using a closure.
The closure receives a &Match and should return the replacement string.
This is useful for dynamic replacements that depend on the match details.
§Examples
use regress::Regex;
let re = Regex::new(r"\d+").unwrap();
let text = "Items: 5, 10, 15";
let result = re.replace_all_with(text, |m| {
let num: i32 = m.as_str(text).parse().unwrap();
format!("[{}]", num * 10)
});
assert_eq!(result, "Items: [50], [100], [150]");