logoalt Hacker News

jeroenhd11/08/20241 replyview on HN

Using parser combinator library "nom", this should probably do what you'd want:

    fn parse_abc(input: &str, n: usize) -> IResult<&str, (Vec<char>, Vec<char>, Vec<char>)> {
      let (input, result) = tuple(( many_m_n(n, n, char('a')),
                                  many_m_n(n, n, char('b')),
                                  many_m_n(n, n, char('c'))
                            ))(input)?;
      Ok((input, result)) 
    }
    
It parses (the beginning of) the input, ensuring `n` repetitions of 'a', 'b', and 'c'. Parse errors are reported through the return type, and the remaining characters are returned for the application to deal with as it sees fit.

https://play.rust-lang.org/?version=stable&mode=debug&editio...


Replies

oguz-ismail11/08/2024

> this should probably do what you'd want

If you have to specify N, no, it doesn't