Skip to content

Instantly share code, notes, and snippets.

Created July 30, 2015 22:29
Show Gist options
  • Save anonymous/cd705476576864f9220e to your computer and use it in GitHub Desktop.
Save anonymous/cd705476576864f9220e to your computer and use it in GitHub Desktop.
extern crate regex;
use std::io::{Write, stderr};
use regex::{Regex, Captures};
type Error = Box<::std::error::Error + Send + Sync>;
fn captures<'t>(pat: &str, txt: &'t str) -> Result<Vec<Captures<'t>>, Error> {
let re = try!(Regex::new(pat));
Ok(re.captures_iter(txt).collect())
}
fn main() {
let caps = match captures(r"(\d+)", "aa 11 bb 22 cc 33") {
Ok(caps) => caps,
Err(err) => {
writeln!(&mut stderr(), "ERROR: {}", err).unwrap();
::std::process::exit(1);
}
};
for cap in &caps {
println!("{:?}", cap.at(1));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment