Crates.io | litepattern |
lib.rs | litepattern |
version | 0.1.0 |
source | src |
created_at | 2017-01-10 19:59:56.237983 |
updated_at | 2017-01-10 19:59:56.237983 |
description | Lightweight pattern matching library for Rust. |
homepage | https://github.com/stpettersens/litepattern |
repository | https://github.com/stpettersens/litepattern |
max_upload_size | |
id | 8012 |
size | 6,160 |
Lightweight pattern matching library for Rust.
# Add to your Cargo.toml file dependencies:
litepattern = "0.1.0"
# or: litepattern = { git = "https://github.com/stpettersens/litepattern.git" }
You can use litepattern
as a lighter alternative to the regex crate, if you only need to do simple pattern matching. For example, say you want to pass a simple timestamp such as 2017-01-10T19:10:00
and break it down into is constituent parts:
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
}