chore: reorg
This commit is contained in:
40
__misc/regex/src/main.rs
Normal file
40
__misc/regex/src/main.rs
Normal file
@@ -0,0 +1,40 @@
|
||||
use regex::Regex;
|
||||
|
||||
const TO_SEARCH: &str = "
|
||||
On 2010-03-14, foo happened. On 2014-10-14, bar happened.
|
||||
";
|
||||
|
||||
fn main() {
|
||||
let re = Regex::new(r"(\d{4})-(\d{2})-(\d{2})").unwrap();
|
||||
|
||||
// year: 2010, month: 03, day: 14
|
||||
// year: 2014, month: 10, day: 14
|
||||
for caps in re.captures_iter(TO_SEARCH) {
|
||||
println!("year: {}, month: {}, day: {}",
|
||||
caps.get(1).unwrap().as_str(),
|
||||
caps.get(2).unwrap().as_str(),
|
||||
caps.get(3).unwrap().as_str());
|
||||
}
|
||||
|
||||
let re2 = Regex::new(r"(\d+)").unwrap();
|
||||
println!("{}", re2.replace_all("Hello 100, 200", | caps: ®ex::Captures | {
|
||||
"(".to_owned() + &(caps[1].parse::<usize>().unwrap() * 2).to_string() + ")"
|
||||
}));
|
||||
println!("{}", re2.replace_all("Hello 100, 200", | caps: ®ex::Captures<'_> | {
|
||||
"[".to_owned() + &caps[1] + "]"
|
||||
}));
|
||||
|
||||
let re3 = Regex::new(r"(?P<x>\d+)").unwrap();
|
||||
println!("{}", re3.replace_all("Hello 100, 200", "<$x>"));
|
||||
|
||||
let e = "name@example.com";
|
||||
let re_e = Regex::new(r"^(\w+)@((\w+)*\.com)$").unwrap();
|
||||
// name -> name
|
||||
// domain -> example.com
|
||||
// domain -> example
|
||||
for cap in re_e.captures_iter(e) {
|
||||
println!("name -> {}", cap.get(1).unwrap().as_str());
|
||||
println!("domain -> {}", cap.get(2).unwrap().as_str());
|
||||
println!("domain -> {}", cap.get(3).unwrap().as_str());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user