0

I've been reading through the documentation for nom 6.2.1 and I'm trying to construct a parser which will match any number of characters (including zero) followed by the word foobar: /.*foobar/.

nom::bytes::complete::take_until("foobar") almost does what I want, but it will not consume foobar itself, so I'd have to instead do:

use nom::bytes::complete::{tag, take_until};
use nom::combinator::value;
use nom::sequence::tuple;
use nom::IResult;

// This method does what I want, but is verbose because foobar is repeated
fn arbitrary_and_then_foobar(s: &str) -> IResult<&str, ()> {
    value((), tuple((take_until("foobar"), tag("foobar"))))(s)
}

I tend to think in terms of the traditional regular expression syntax and then have to map that to the available constructs that nom provides. I'm getting better at noticing scenarios where the nom constructs fit better, but is there a regex-to-nom cheatsheet?

Huckle
  • 1,810
  • 3
  • 26
  • 40
  • Many languages use `//` to mark a regex literal in source code. Since Rust does not, your phrasing is potentially more unclear than it needs to be — do you want to match the literal characters / string `/`? – Shepmaster Jun 30 '21 at 18:49
  • https://docs.rs/nom/6.2.1/nom/sequence/fn.terminated.html ? – Shepmaster Jun 30 '21 at 18:50
  • @Shepmaster, can you elaborate? I don't have anything specific to capture as the first argument, other than it not matching `foobar`. – Huckle Jun 30 '21 at 19:07
  • Nom does have a `regexp` feature that lets you parse with regular expressions if you can't come up with a better solution. – Aiden4 Jun 30 '21 at 20:53
  • I do not speak regex, mainly because regex is a mess, so please describe what you want with word or example. – Stargateur Jun 30 '21 at 22:56

0 Answers0