You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

23 lines
524 B

extern crate regex;
use regex::Regex;
fn main() {
let args: Vec<String> = std::env::args().collect();
if args.len() < 4 {
println!("Usage: {} <str> <regex> <group>", args[0]);
} else {
let re = Regex::new(&args[2]).unwrap();
let text = &args[1];
let group: usize = args[3].parse().expect("Invalid group number.");
let groups = re.captures(&text).unwrap();
if group > groups.len() {
eprintln!("Invalid group number.");
} else {
println!("{}", groups.get(group).unwrap().as_str());
}
}
}