Partial Declarations from regex crate
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub struct Match<'t> {
text: &'t str,
start: usize,
end: usize,
}
pub struct Regex(Exec);
/* compiles reg express'n, result can be used repeatedly */
pub fn new(re: &str) -> Result<Regex, Error>
/* cheapest way to detect a match */
pub fn is_match(&self, text: &str) -> bool
/* returns start and end of first match if it exists */
pub fn find<'t>(&self, text: &'t str) -> Option<Match<'t>>
/* returns iterator for successive non-overlapping matches */
pub fn find_iter<'r, 't>(&'r self, text: &'t str) -> Matches<'r, 't>
/* returns capture groups for first match in text */
pub fn captures<'t>(&self, text: &'t str) -> Option<Captures<'t>>
/* returns iterator over all non-overlapping capture groups */
pub fn captures_iter<'r, 't>(
&'r self,
text: &'t str,
) -> CaptureMatches<'r, 't>
/* returns iterator of substrings of matching text */
pub fn split<'r, 't>(&'r self, text: &'t str) -> Split<'r, 't>
/* replaces first match with replacement */
pub fn replace<'t, R: Replacer>(
&self,
text: &'t str,
rep: R,
) -> Cow<'t, str>
/* replaces all non-overlapping matches in text with replacement */
pub fn replace_all<'t, R: Replacer>(
&self,
text: &'t str,
rep: R,
) -> Cow<'t, str>
Contents of the preceding block show the structure and methods of regex::Regex. The
next block gives examples of their use, and Section 3.0 discusses regular expression pattern
syntax and semantics.
Example Use
use regex::{Regex, Match, Captures};
fn check(pattern: &str, text: &str, pred:bool) {
if pred {
print!(
"\n pattern: {:?} matches text: {:?}",
pattern, text
);
}
else {
print!(
"\n pattern: {:?} !matches text: {:?}",
pattern, text
);
}
}
fn range(pattern: &str, text: &str, mat: &Option<Match>
) {
if let Some(mt) = mat {
print!(
"\n find pattern {:?} in text {:?}:",
pattern, text
);
print!(" match in [{}, {})", mt.start(), mt.end());
}
else {
print!("\n no match");
}
}
fn range_iter(pattern: &str, text: &str, mat: Match) {
print!(
"\n find pattern {:?} in text {:?}:",
pattern, text
);
print!(" match in [{}, {})", mat.start(), mat.end());
}
fn test_match() {
print!("\n -- test_match --");
let pattern = r"[a-q]{3,4}$";
let re = Regex::new(pattern).unwrap();
let text = "12cde";
let pred = re.is_match(text);
check(pattern, text, pred);
let text = "12cdefg";
let pred = re.is_match(text);
check(pattern, text, pred);
let text = "12cd";
let pred = re.is_match(text);
check(pattern, text, pred);
let text = "12cd3e";
let pred = re.is_match(text);
check(pattern, text, pred);
let text = "12cds";
let pred = re.is_match(text);
check(pattern, text, pred);
}
fn test_find() {
print!("\n -- test_find --");
let pattern = r"abc";
let re = Regex::new(pattern).unwrap();
let text = "123abc456";
let op: Option<Match> = re.find(text);
range(pattern, text, &op);
}
fn test_find_iter() {
print!("\n -- test_find_iter --");
let pattern = r"abc";
let re = Regex::new(pattern).unwrap();
let text = "123abc456abc789";
let matches = re.find_iter(text);
for mat in matches {
range_iter(pattern, text, mat);
}
}
fn test_captures() {
print!("\n -- test_captures --");
let text = "123abc456def789";
let pattern = "\
([a-z]{3}|[0-9]{3})\
([a-z]{3}|[0-9]{3})\
([a-z]{3}|[0-9]{3})\
([a-z]{3}|[0-9]{3})\
([a-z]{3}|[0-9]{3})\
";
// These don't work as you might expect.
// Capture doesn't work well with repetitions.
// let pattern = r"([a-z]{3}|[0-9]{3}){5}";
// let pattern = r"(([a-z]{3})([0-9]{3}))+";
// let pattern = r"((?:\d+)+)+";
let re = Regex::new(pattern).unwrap();
let captures: Option<Captures> = re.captures(text);
print!("\n captures: {:?}", captures);
let caps = captures.unwrap();
for i in 0..caps.len() {
print!("\n captures[{}] = {:?}", i, &caps.get(i));
let cap = &caps.get(i).unwrap();
print!(
"\n cap = {:?}, {}, {}",
cap.as_str(), cap.start(), cap.end()
);
}
}
fn main() {
test_match();
test_find();
test_find_iter();
test_captures();
}
Output
-- test_match --
pattern: "[a-q]{3,4}$" matches text: "12cde"
pattern: "[a-q]{3,4}$" matches text: "12cdefg"
pattern: "[a-q]{3,4}$" !matches text: "12cd"
pattern: "[a-q]{3,4}$" !matches text: "12cd3e"
pattern: "[a-q]{3,4}$" !matches text: "12cds"
-- test_find --
find pattern "abc" in text "123abc456": match in [3, 6)
-- test_find_iter --
find pattern "abc" in text "123abc456abc789":
match in [3, 6)
find pattern "abc" in text "123abc456abc789":
match in [9, 12)
-- test_captures --
captures: Some(
Captures({
0: Some("123abc456def"),
1: Some("123"),
2: Some("abc"),
3: Some("456"),
4: Some("def")
})
)
captures[0] = Some(
Match {
text: "123abc456def789", start: 0, end: 12
}
)
cap = "123abc456def", 0, 12
captures[1] = Some(
Match {
text: "123abc456def789", start: 0, end: 3
}
)
cap = "123", 0, 3
captures[2] = Some(
Match {
text: "123abc456def789", start: 3, end: 6
}
)
cap = "abc", 3, 6
captures[3] = Some(
Match {
text: "123abc456def789", start: 6, end: 9
}
)
cap = "456", 6, 9
captures[4] = Some(
Match {
text: "123abc456def789", start: 9, end: 12
}
)
cap = "def", 9, 12
captures[5] = Some(
Match {
text: "123abc456def789", start: 12, end: 15
}
)
cap = "789", 12, 15