add regex
This commit is contained in:
@@ -7,6 +7,8 @@ On 2010-03-14, foo happened. On 2014-10-14, bar happened.
|
|||||||
fn main() {
|
fn main() {
|
||||||
let re = Regex::new(r"(\d{4})-(\d{2})-(\d{2})").unwrap();
|
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) {
|
for caps in re.captures_iter(TO_SEARCH) {
|
||||||
println!("year: {}, month: {}, day: {}",
|
println!("year: {}, month: {}, day: {}",
|
||||||
caps.get(1).unwrap().as_str(),
|
caps.get(1).unwrap().as_str(),
|
||||||
@@ -24,4 +26,15 @@ fn main() {
|
|||||||
|
|
||||||
let re3 = Regex::new(r"(?P<x>\d+)").unwrap();
|
let re3 = Regex::new(r"(?P<x>\d+)").unwrap();
|
||||||
println!("{}", re3.replace_all("Hello 100, 200", "<$x>"));
|
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