Skip to content

stpettersens/litepattern

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

47 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

litepattern

Lightweight pattern matching library for Rust.

Build Status Build status MIT License Crates.io

# 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 its 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
}

Documentation

Releases

No releases published

Packages

No packages published

Languages