Function captures

Source
pub fn captures<'h, Input, Re, Error>(
    re: Re,
) -> CapturesParser<'h, Input, Re::Output, Error>
where Input: StreamIsPartial + Stream + Offset + Clone, Re: BytesRegexPattern, Re::Output: Regex<Haystack<'h> = <Input as Stream>::Slice>, Re::Error: Debug, Error: ParserError<Input> + 'static,
Expand description

A &[u8]-oriented version of [winnow_regex::captures].

This parser matches and extracts capture groups from the beginning of a byte stream (&[u8]) using a regular expression compiled with regex::bytes::Regex. If the regex matches at offset 0, all capture groups are returned.

For full semantics and error behavior, see [winnow_regex::captures].

§Panics

Panics if the regex pattern fails to compile.

§Example

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

fn coords(input: &mut &[u8]) -> ModalResult<(u32, u32)> {
    captures(r"^(\d+),(\d+)")
        .map(|c| {
            let x = std::str::from_utf8(&c[1]).unwrap().parse().unwrap();
            let y = std::str::from_utf8(&c[2]).unwrap().parse().unwrap();
            (x, y)
        })
        .parse_next(input)
}

assert_eq!(coords.parse_peek(b"42,99 done"), Ok((&b" done"[..], (42, 99))));