added feature "perl": use pcre

master
Avril 5 years ago
parent 5eb377769e
commit ab97713013
Signed by: flanchan
GPG Key ID: 284488987C31F630

@ -6,5 +6,9 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[features]
perl = ["pcre"]
[dependencies]
regex = "1"
pcre = { version = "0.2.3", optional = true }

@ -1,6 +1,10 @@
#[cfg(not(feature = "perl"))]
extern crate regex;
#[cfg(not(feature = "perl"))]
use regex::Regex;
#[cfg(not(feature = "perl"))]
fn main() {
let args: Vec<String> = std::env::args().collect();
@ -11,12 +15,42 @@ fn main() {
let text = &args[1];
let group: usize = args[3].parse().expect("Invalid group number.");
let groups = re.captures(&text).unwrap();
match re.captures(&text) {
if group > groups.len() {
eprintln!("Invalid group number.");
} else {
println!("{}", groups.get(group).unwrap().as_str());
Some(groups) => {
if group > groups.len() {
eprintln!("Invalid group number.");
} else {
println!("{}", groups.get(group).unwrap().as_str());
}
}
None => (),
}
}
}
#[cfg(feature = "perl")]
extern crate pcre;
#[cfg(feature = "perl")]
use pcre::Pcre;
#[cfg(feature = "perl")]
fn main () {
let args: Vec<String> = std::env::args().collect();
if args.len() < 4 {
println!("Usage: {} <str> <regex> <group>", args[0]);
} else {
let mut re = Pcre::compile(&args[2]).expect("Invalid regex.");
let text = &args[1];
let group: usize = args[3].parse().expect("Invalid group number.");
match re.exec(&text) {
Some(m) if m.string_count() > group => println!("{}", m.group(group)),
_ => (),
}
}
}

Loading…
Cancel
Save