1 个不稳定版本
使用旧的 Rust 2015
0.1.0 | 2017 年 1 月 10 日 |
---|
#75 in #matching
26 次每月下载
6KB
63 行
litepattern
轻量级 Rust 模式匹配库。
# Add to your Cargo.toml file dependencies:
litepattern = "0.1.0"
# or: litepattern = { git = "https://github.com/stpettersens/litepattern.git" }
如果您只需要进行简单的模式匹配,可以使用 litepattern
作为对 regexcrate 的轻量级替代品。例如,假设您想要传递一个简单的时间戳,如 2017-01-10T19:10:00
并将其分解为其组成部分
extern crate litepattern;
use litepattern::LPattern;
fn main() {
// Parse something like 2017-01-10T19:10:00.
// The % is mandatory, but the d is just notation for a digit, you can use another non-"%" character.
let p = LPattern::new("%dddd-%dd-%ddT%dd-%dd-%dd"); // => LPattern.
// Apply the pattern against ("to") some input text and return any matches (captures) as a vector of Strings.
let caps = p.apply_to("2017-01-10T19:10:00"); // => ["2017-", "01-", "10T", "19:", "10:", "00"]
// Get the year.
println!("{}", &caps[0][0..4]); // First item in vector; slice of four characters from index zero => 2017
// Get the month.
println!("{}", &caps[1][0..2]); // Second item in vector; slice of two characters from index zero => 01
// Get the day.
println!("{}", &caps[2][0..2]); // Third item in vector; slice of two characters from index zero => 10
}