Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

30 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Regex-Parse

A macro to parse simple inputs using regular expressions

Documentation Build status

Installation

cargo add --git https://github.com/Inky-developer/regex-parse regex-parse

Example

Parsing a date:

use regex_parse::re_parse;

fn main() {
    let input = "2024-12-15";
    let year: u32;
    let month: u32;
    let day: u32;
    re_parse!("{year}\\-{month}\\-{day}", input);
    assert_eq!(year, 2024);
    assert_eq!(month, 12);
    assert_eq!(day, 15);
}

Using regular expressions:

use regex_parse::re_parse;

fn main() {
    let inputs = ["1 2 3 4", "-5      6 -7"];
    let parsed_inputs = inputs.map(|input| {
        let numbers: Vec<i32>;
        re_parse!(r"({numbers*}\s*)*", input);
        numbers
    });
    assert_eq!(parsed_inputs, [vec![1, 2, 3, 4], vec![-5, 6, -7]]);
}

Regex Features

  • literal text: abcdef
  • variables: abc{var}def
  • or: a|b
  • parenthesis: (ab)|(cd)
  • any character in group: [abc]
  • any character in range: [a-z]
  • any character not in group: [^abc]
  • any character: .
  • any whitespace: \s
  • any digit: \d
  • any word: \w
  • zero or one: a?
  • zero or more: a*
  • one or more: a+
  • exactly n: a{3}
  • n or more: a{3,}
  • between n and m: a{3,6}

About

A simple macro to parse input with a regular expression

Topics

Resources

Stars

1 star

Watchers

1 watching

Forks

Used by

Contributors

Languages