#regex #capture #winnow-regex

winnow-regex

A set of winnow parsers backed by the regex crate

3 releases

Uses new Rust 2024

new 0.1.2 May 6, 2025
0.1.1 May 6, 2025
0.1.0 May 5, 2025

#29 in #capture

Download history

55 downloads per month

MIT license

15KB
308 lines

winnow-regex

crates.io docs.rs

A set of winnow parsers backed by the regex crate.
Provides two generic parsers:

  • regex(pattern) – match a slice against a regular expression from the beginning.
  • captures(pattern) – match and return captured groups.

Both parsers support complete‑and‑partial streaming via the StreamIsPartial trait.

Quick Start

use winnow::prelude::*;
use winnow_regex::regex;

fn digits<'i>(input: &mut &'i str) -> ModalResult<&'i str> {
    // matches one or more digits at the front
    regex(r"^\d+").parse_next(input)
}

assert_eq!(digits.parse_peek("42abc"), Ok(("abc", "42")));
assert!(digits.parse_peek("abc42").is_err());

Captures Example

use winnow::prelude::*;
use winnow_regex::{captures, Captures};

fn dims<'i>(input: &mut &'i str) -> ModalResult<(i32, i32)> {
    // captures two number groups: width and height
    captures(r"^(\d+)x(\d+)")
        .map(|caps| {
            let w: i32 = caps[1].parse().unwrap();
            let h: i32 = caps[2].parse().unwrap();
            (w, h)
        })
        .parse_next(input)
}

assert_eq!(dims.parse_peek("800x600rest"), Ok(("rest", (800, 600))));

Dependencies

~3.5–5MB
~95K SLoC