chore: reorg
This commit is contained in:
58
__misc/regex/Cargo.lock
generated
Normal file
58
__misc/regex/Cargo.lock
generated
Normal file
@@ -0,0 +1,58 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
[[package]]
|
||||
name = "aho-corasick"
|
||||
version = "0.7.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"memchr 2.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "lazy_static"
|
||||
version = "1.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "memchr"
|
||||
version = "2.2.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "regex"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"regex 1.3.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "regex"
|
||||
version = "1.3.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"aho-corasick 0.7.6 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"memchr 2.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"regex-syntax 0.6.12 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"thread_local 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "regex-syntax"
|
||||
version = "0.6.12"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "thread_local"
|
||||
version = "0.3.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[metadata]
|
||||
"checksum aho-corasick 0.7.6 (registry+https://github.com/rust-lang/crates.io-index)" = "58fb5e95d83b38284460a5fda7d6470aa0b8844d283a0b614b8535e880800d2d"
|
||||
"checksum lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
|
||||
"checksum memchr 2.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "88579771288728879b57485cc7d6b07d648c9f0141eb955f8ab7f9d45394468e"
|
||||
"checksum regex 1.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "dc220bd33bdce8f093101afe22a037b8eb0e5af33592e6a9caafff0d4cb81cbd"
|
||||
"checksum regex-syntax 0.6.12 (registry+https://github.com/rust-lang/crates.io-index)" = "11a7e20d1cce64ef2fed88b66d347f88bd9babb82845b2b858f3edbf59a4f716"
|
||||
"checksum thread_local 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "c6b53e329000edc2b34dbe8545fd20e55a333362d0a321909685a19bd28c3f1b"
|
||||
13
__misc/regex/Cargo.toml
Normal file
13
__misc/regex/Cargo.toml
Normal file
@@ -0,0 +1,13 @@
|
||||
[package]
|
||||
name = "regex"
|
||||
version = "0.1.0"
|
||||
authors = ["Hatter Jiang <jht5945@gmail.com>"]
|
||||
edition = "2018"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
regex = "1.3.1"
|
||||
|
||||
|
||||
|
||||
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