Vec<u8> で使う場合は String に変換する

// s は Vec<u8>。もとからString であればこんなことしなくて良い。
let re = Regex::new(r"^<=+>$").unwrap();
let ans = re.is_match(&String::from_utf8(s.clone()).unwrap());

置換

replace の型はこう

pub fn replace<'h, R: Replacer>(
    &self,
    haystack: &'h str,
    rep: R
) -> Cow<'h, str>

find


let re = Regex::new(r"\\b\\w{13}\\b").unwrap();
let hay = "I categorically deny having triskaidekaphobia.";
let mat = re.find(hay).unwrap();
assert_eq!(2..15, mat.range());
assert_eq!("categorically", mat.as_str());

場所が range で取得できる。マッチした文字列も取得できる。

find_iter

let re = Regex::new(r"[0-9]{4}-[0-9]{2}-[0-9]{2}").unwrap();
let hay = "What do 1865-04-14, 1881-07-02, 1901-09-06 and 1963-11-22 have in common?";
// 'm' is a 'Match', and 'as_str()' returns the matching part of the haystack.
let dates: Vec<&str> = re.find_iter(hay).map(|m| m.as_str()).collect();
assert_eq!(dates, vec![
    "1865-04-14",
    "1881-07-02",
    "1901-09-06",
    "1963-11-22",
]);

キャプチャー