pub struct Regex { /* private fields */ }Expand description
A compiled regular expression.
This is the main entry point for pattern matching. A Regex can be used
concurrently from multiple threads.
§Examples
use regexr::Regex;
let re = Regex::new(r"\d{4}-\d{2}-\d{2}").unwrap();
assert!(re.is_match("2024-01-15"));Implementations§
Source§impl Regex
impl Regex
Sourcepub fn builder() -> RegexBuilder
pub fn builder() -> RegexBuilder
Create a regex builder for advanced configuration.
§Example
use regexr::Regex;
let re = Regex::builder()
.pattern(r"\d+")
.case_insensitive(true)
.vm()
.build()
.unwrap();Sourcepub fn is_match(&self, text: &str) -> bool
pub fn is_match(&self, text: &str) -> bool
Returns true if the pattern matches anywhere in the input text.
§Examples
use regexr::Regex;
let re = Regex::new(r"\d+").unwrap();
assert!(re.is_match("abc123"));
assert!(!re.is_match("no digits"));Sourcepub fn is_match_at_start(&self, text: &str) -> bool
pub fn is_match_at_start(&self, text: &str) -> bool
Returns true if the pattern matches at the start of the input text.
This is equivalent to anchoring the pattern with ^, but may be faster.
Sourcepub fn find<'t>(&self, text: &'t str) -> Option<Match<'t>>
pub fn find<'t>(&self, text: &'t str) -> Option<Match<'t>>
Find the first match in the input text.
§Examples
use regexr::Regex;
let re = Regex::new(r"\d+").unwrap();
if let Some(m) = re.find("abc123def") {
assert_eq!(m.as_str(), "123");
assert_eq!(m.start(), 3);
assert_eq!(m.end(), 6);
}Sourcepub fn find_at<'t>(&self, text: &'t str, start: usize) -> Option<Match<'t>>
pub fn find_at<'t>(&self, text: &'t str, start: usize) -> Option<Match<'t>>
Find a match starting at the given byte offset.
§Panics
Panics if start is greater than text.len() or is not on a UTF-8
character boundary.
Sourcepub fn find_iter<'r, 't>(&'r self, text: &'t str) -> FindIter<'r, 't>
pub fn find_iter<'r, 't>(&'r self, text: &'t str) -> FindIter<'r, 't>
Returns an iterator over all non-overlapping matches.
§Examples
use regexr::Regex;
let re = Regex::new(r"\d+").unwrap();
let matches: Vec<_> = re.find_iter("a1b22c333").map(|m| m.as_str()).collect();
assert_eq!(matches, vec!["1", "22", "333"]);Sourcepub fn captures<'t>(&self, text: &'t str) -> Option<Captures<'t>>
pub fn captures<'t>(&self, text: &'t str) -> Option<Captures<'t>>
Returns the capture groups for the first match.
§Examples
use regexr::Regex;
let re = Regex::new(r"(\w+)@(\w+)\.(\w+)").unwrap();
if let Some(caps) = re.captures("user@example.com") {
assert_eq!(&caps[0], "user@example.com");
assert_eq!(&caps[1], "user");
assert_eq!(&caps[2], "example");
assert_eq!(&caps[3], "com");
}Sourcepub fn captures_at<'t>(
&self,
text: &'t str,
start: usize,
) -> Option<Captures<'t>>
pub fn captures_at<'t>( &self, text: &'t str, start: usize, ) -> Option<Captures<'t>>
Returns capture groups starting at the given byte offset.
Sourcepub fn captures_iter<'r, 't>(&'r self, text: &'t str) -> CapturesIter<'r, 't>
pub fn captures_iter<'r, 't>(&'r self, text: &'t str) -> CapturesIter<'r, 't>
Returns an iterator over all capture groups for all matches.
Sourcepub fn replace<'t>(&self, text: &'t str, rep: &str) -> Cow<'t, str>
pub fn replace<'t>(&self, text: &'t str, rep: &str) -> Cow<'t, str>
Replace the first match with the replacement string.
§Examples
use regexr::Regex;
let re = Regex::new(r"\d+").unwrap();
assert_eq!(re.replace("abc123", "XXX"), "abcXXX");Sourcepub fn replace_all<'t>(&self, text: &'t str, rep: &str) -> Cow<'t, str>
pub fn replace_all<'t>(&self, text: &'t str, rep: &str) -> Cow<'t, str>
Replace all matches with the replacement string.
§Examples
use regexr::Regex;
let re = Regex::new(r"\d+").unwrap();
assert_eq!(re.replace_all("a1b2c3", "X"), "aXbXcX");Sourcepub fn split<'r, 't>(&'r self, text: &'t str) -> Split<'r, 't>
pub fn split<'r, 't>(&'r self, text: &'t str) -> Split<'r, 't>
Split the input text by the pattern.
§Examples
use regexr::Regex;
let re = Regex::new(r"[ ,]+").unwrap();
let parts: Vec<_> = re.split("a, b, c").collect();
assert_eq!(parts, vec!["a", "b", "c"]);Sourcepub fn captures_len(&self) -> usize
pub fn captures_len(&self) -> usize
Returns the number of capture groups in the pattern.
Sourcepub fn engine_info(&self) -> EngineInfo
pub fn engine_info(&self) -> EngineInfo
Returns information about the selected engine.
This is useful for debugging and performance analysis.